11

what is the right way to free an allocated memory after executing function in C (via malloc)? I need to alloc memory, use it somehow and return it back, than I have to free it.

char* someFunction(char* a, char* b) {
   char* result = (char*)malloc(la + 2 * sizeof(char));
   ...
   return result;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Jax-p
  • 7,225
  • 4
  • 28
  • 58
  • 9
    `char *str = someFunction(a, b); useitsomehow(str); free(str);` – David Ranieri Mar 01 '16 at 08:32
  • Well, this means that I will return the pointer, which is avaible to free as "str" because it is now the exactly same pointer? – Jax-p Mar 01 '16 at 08:40
  • 3
    @JaxCze: Yes, also I would not call it "exactly the same pointer". It is a different (pointer) variable with exactly the same value, i.e. pointing to the same object. – undur_gongor Mar 01 '16 at 08:44
  • If you return memory that the caller has to free (and if you can't avoid this), then you should at least make this clear with a comment like: "The caller is responsible for freeing the returned memory". – Marco13 Mar 01 '16 at 09:46
  • You don't free a *variable*, you free some *pointer* (which sits in a variable, or is returned by some function or expression). Be aware of [pointer aliasing](https://en.wikipedia.org/wiki/Pointer_aliasing). You need to define and document *conventions* about who and when a pointer should be freed. Some tools (e.g. [valgrind](http://valgrind.org/)...) might help to find [memory leaks](https://en.wikipedia.org/wiki/Memory_leak) – Basile Starynkevitch Mar 12 '16 at 10:07

5 Answers5

10

Use free. In your case, it will be:

char* result = malloc(la + 2 * sizeof(char));
...
free (result);

Also, if you're returning allocated memory, like strdup does, the caller of your function has to free the memory. Like:

result = somefunction ();
...
free (result);

If you're thinking of freeing it after returning it, that is not possible. Once you return something from the function, it automatically gets terminated.

Box Box Box Box
  • 5,094
  • 10
  • 49
  • 67
  • 6
    This code snippet looks so sketchy, I hope there are no misunderstandings: You should **NOT** `free` the memory before returning it! – Marco13 Mar 01 '16 at 09:46
6

In the code that called someFunction.

You also have to make clear in the documentation (you have that, right?!), that the caller has to call free, after finished using the return value.

Koshinae
  • 2,240
  • 2
  • 30
  • 40
3

If you return allocated memory, then it is the caller responsibility to free it.

char *res;
res = someFunction("something 1", "something 2");
free(res);
Igal S.
  • 13,146
  • 5
  • 30
  • 48
3

Well you return it to calling function , then just free the pointer in calling function.

ameyCU
  • 16,489
  • 2
  • 26
  • 41
3

If you mean that the returned value is not needed in the calling function and the function is called due to its side effects then you can just write

free( someFunction( p, q ) );
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335