Memory management in C embedded hardware [on hold]

问题内容:

Embedded hardware has limited memory and requires memory management. I would like to know what is the most effective way in returning a result from a function? I have a few example functions as below:

first function:

char* somefunction(){
    char* result;
    int error = anotherfunction();
    if(error == 0){
       strcpy(result, "succeed");
    } else{
       strcpy(result, "fail");
    }
    return result;
}

second function:

char* somefunction(){
    char* result = malloc(sizeof(char) * 10);
    int error = anotherfunction();
    if(error == 0){
       strcpy(result, "succeed");
    } else{
       strcpy(result, "fail");
    }
    return result;                     //where to free(result)?
}

third function:

void somefunction(char* result){         //malloc and free(result) from calling function?
    int error = anotherfunction();
    if(error == 0){
       strcpy(result, "succeed");
    } else{
       strcpy(result, "fail");
    }
    return result;                     
}

1) which function is the most effective in memory management for returning the variable ‘result’? If none of the above, what is an example of the most effective way?

2) In the first function, does ‘char* result’ allocates any memory to it? Or it allocates temporary only when it enter the function and is deleted automatically once it exits function?

3) In the second function, where do I free the ‘result’ variable?

4) In the third function, do I do malloc and free from the calling function? and is it possible to change the ‘result’ variable in this way?

5) If I want to copy the ‘error’ variable value (if changed to string) to result, will using ‘char* result’ still works? (I read that pointer to string without malloc is use for read-only, not able to change it?)

Thank you.

问题评论:

    
The choices between 2 and 3 are dependent on other aspects of your design that you do not describe. 1 is borked.
3  
All 3 are rubbish. Throw them all out and just return true or false (or 1 and 0 if you want to be pedantic and handle all C versions)
    
..besides, why not return a success/fail boolean or an int error number? An int could be 0 for success and some index of a const string array for error texts, so avoiding the storage of text in RAM altogether.
    
@Martin James no sgfaults in the bare metal uC programme. But you are 100%right. OP needs to learn C first.
    
All you approaches are wrong. For the string literals there is no need to waste the RAM, as they are already stored in the flash memory. Just return the pointer to the literal. Dynamic memory allocation is a very complex topic in the bare metal programming and is not as obvious and easy as on the hodted systems with almost unlimited resources. Try to avoid – many automobile or aerospace standards do not allow any dynic allocation at all. In the systems which are on for a longer time and do not have any virtual memory capabilities it leads to the memory segmentation

答案:

答案1:

In your particular case it makes absolutely no sense to return strings, in particular in the embedded software. You can return an ‘int’ indicating error/no error. This would be the most efficient way.

int somefunction(char* result){ 
    int error = anotherfunction();
    return (error != 0); // 0 -- success, 1 -- failure
 }

you do not even need the somefunction in such a case, just call to the anotherfunction from the caller, if you care about efficiency.

However, if you insist,

  1. this function will just corrupt memory since you did not allocate any storage for strings
  2. this is a usual way for doing such things. But you have to free the memory int the caller.
  3. This depends. If the caller allocated enough memory for it, you could be ok, however, the caller has to free the memory (#2). If it did not allocate memory, you are at #1, corrupting the memory.

If you return arbitrary strings, you can still corrupt memory by overflowing the allocated size, so you need to guarantee that you allocated enough memory, and/or use function which will not overflow the buffers, i.e. strncpy instead of strcpy.

So, out of 3 functions, first is rubbish, the second 2 are equivalent if used correctly. The rest depends on your personal preference.

答案评论:

原文地址:

https://stackoverflow.com/questions/47745555/memory-management-in-c-embedded-hardware

添加评论