2

Is it good practice to malloc in the caller function, and then free in the callee function?

Example:

int main() {

    int *ptr = malloc(100);
    foo(ptr);
    return 0;
}

void foo(int *ptr) {
    free(ptr);
}

Kyoma
  • 139
  • 2
  • https://stackoverflow.com/q/2998167 – Robert Harvey Jun 17 '19 at 02:18
  • https://stackoverflow.com/q/13442375 – Robert Harvey Jun 17 '19 at 02:19
  • https://www.reddit.com/r/C_Programming/comments/2wu0i5/is_it_better_to_malloc_inside_a_function_or/ – Robert Harvey Jun 17 '19 at 02:21
  • @RobertHarvey Thks for the links, but I am more interested in free, rather than malloc. In the example given, should I free it in main() or free it in foo()? – Kyoma Jun 17 '19 at 02:24
  • Well, if the function you're calling is responsible for malloc'ing, then it should either free the memory itself or return a pointer to the memory so allocated. – Robert Harvey Jun 17 '19 at 02:29
  • Ergo, it seems to me that the decision to be made is not where to free the memory (since that seems self-evident), but rather where to allocate it. – Robert Harvey Jun 17 '19 at 02:30
  • Add mentioned, it's fine, but not always the best approach. If it's hard to determine the required lifetime (e.g. asynchronous task), I'd add some kind of optional callback you can pass, which would then take cleanup responsibility. – Mario Jun 17 '19 at 06:48

2 Answers2

5

It is valid C code to call malloc in main and free in foo, but it is not considered best practice.

If allocation and deallocation are done in different, unrelated, functions, then it becomes that much harder to tell if every block of memory obtained from malloc is actually freed (to prove you don't have a memory leak) or to prove that every pointer passed to free actually comes from malloc (which is a requirement for using free).

Bart van Ingen Schenau
  • 71,712
  • 20
  • 110
  • 179
1

In your specific context? I'd ask why you could not simply pass int down by value...

But lets assume you are passing some complicated struct bar, that has the property of not being a nice stack resident... That's absolutely fine... Just ensure that the function foo(bar*) is clearly marked as taking responsibility for bar and that it will fulfil that responsibility by calling free(bar) later.

That last part is important as many applications have more than just malloc/free memory management. Often you will also have memory mapped files, system heaps, perhaps even specialised allocators.

Why this is important is that passing a Bar* obtained from a memory mapped file to foo(bar*) is actually illegal, as it is illegal to pass it to free(bar*). On some systems this will be ok, nothing bad happens (because you are lucky). On other systems prepare for debugging woes... poor, poor soul.

The important question here is not: Is this good practice? But: Have I ensured each piece of memory is being appropriately managed?

As an aside, to illustrate that your question holds its own answer, I would like to challenge you to use free(...) without passing it a pointer...

Kain0_0
  • 15,888
  • 16
  • 37