5

Old question, but I still have some thoughts though.

char * getarrmal(void)
{
    char *str;
    str = (char *)malloc(10);
    str[0] = 'a';
    str[1] = 'b';
    str[2] = 'c';
    str[3] = '\0';
    return str;
}

char * getarrdef(void)
{
    char *str = "hello";
    return str;
}

char * getarrfix(void)
{
    char str[10] = "world";
    return str;
}

Three functions. First two will return the string address and the string is stored in heap so that you can continue use it in, for example main() function.

In last function the str is a local variable and the returned str cannot be used.

My question is, when I return in the function which is calling the first two, should I manually free them? It is easy to believe for the malloc case it is true, but I am not sure if it is also the case for char *str = "hello".

If I use getarrdef() and not free its return value, then will I have memory leakage somehow?

1
  • 3
    If you allocated it, and you didn't use malloc (or one of its cousins) to do so, you don't call free. Simple. Commented Apr 24, 2013 at 3:44

1 Answer 1

4

No, you should definitely not try to free the second one. It is not stored in the heap, rather it's a string literal, and trying to free it is undefined behaviour. From C++11 7.21.3.3 The free function:

... if the argument does not match a pointer earlier returned by a memory management function, or if the space has been deallocated by a call to free or realloc, the behavior is undefined.

For the first one, yes, it's good practice for the responsibility of allocated memory to pass along with the memory itself. That means if something allocates it and gives it to you, you are then responsible for freeing it.

That's the case even if you free it by passing it back to a function like: delarrmal() - by doing that, you've given that function responsibility to free it.

Sign up to request clarification or add additional context in comments.

11 Comments

Thanks for your quick reply:). Then it is a little bit interesting where is the str stored in the second function, the caller's stack?
@coldguy, str (the pointer) is stored on the stack in the first two cases, and str (the array) is stored there for the third case. However, what the pointer points to differs in the first two cases (the third case is irrelevant since str is not a pointer (within the function)). In the first case, it points to the heap, in the second it points to a string literal not in the heap.
The function or module that allocated memory should free it; passing stuff around to be freed by others is an excellent way to leak memory or crash on double free. But what's most important is to nail down and document the ownership semantics. Of course, if you give up on the obsolete language C, passing ownership around can be done safely, as with C++11's unique_ptr.
@coldguy string literals are neither in the heap nor on the stack; in most implementations they are in the same place as static and global variables.
Or, the second function will be the best practice. You can return a string, and you don't need to worry about its memory allocation. Seems wonderful...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.