4

I would be very glad if someone could help me in understanding completely the differences between the following codes:

// Code (1)
void f1 ( void ){
  int * ptr1 = malloc ( sizeof(int) );
}

and

// Code (2)
void f2 ( void ){
  int * ptr2 = malloc ( sizeof(int) );
  free(ptr2);
}

As far I know, the instruction free is useful for deallocating the used memory, but on the other hand I know that every time we call a function g, if here there are new variables to be declared, they will be created and destroyed (i.e. deallocated, right?) after the execution of g.

Consequently:

do we need to use the instruction free in Code(2), or it is superfluous? (but maybe suggested for making the code more clear)

Thanks

Ps: you may be also interested in helping me with My previous related question. There, some users suggested to use the dynamic allocation of memory, and I am trying to understand why/how to.

Biagio
  • 131
  • 1
  • 6
  • 2
    No, you will have to use `free` – CoderPi Dec 20 '15 at 16:40
  • Note that there is another allocation function, [`alloca`](http://linux.die.net/man/3/alloca), which *does* automatically free memory at the end of the calling function. However, I've never seen it used. I think it's because most developers agree that the extra bookkeeping required to support `alloca` doesn't justify it's use, as opposed to just making sure your `malloc` and `free` calls are properly paired. – DaoWen Dec 20 '15 at 17:28
  • @DaoWen: `alloca()` is not described in the C Standard, or POSIX. – pmg Dec 20 '15 at 17:40

5 Answers5

9

No. malloc will not free the allocated memory. You need to use free to free the allocated chunk.
It is also the case that at the end of the program (main function) the allocated memory automatically freed by the system, but better to free it explicitly.

Always remember that the lifetime of dynamically allocated memory is till the end of the program if it is not deallocated specifically.

haccks
  • 104,019
  • 25
  • 176
  • 264
  • Thank you, this is very interesting by my point of view! So, the general rule "every new variable created in a function will be destroyed at the end" is false, and the "malloc case" is an important exception, right? – Biagio Dec 20 '15 at 16:44
  • @Biagio; Yes. If a `staic` local variable is there then also its lifetime will be end of the program. – haccks Dec 20 '15 at 16:46
  • Hi, is malloc() memory in heap or stack or else where ? – Louise Apr 07 '19 at 03:35
  • 1
    @Louise; Dynamic memory allocated on heap while static memory allocated on stack. – haccks Apr 07 '19 at 06:20
6

As said previously, memory allocated by malloc should be unallocated by free, or else you'll get memory leaks. What confuses you is that the ptr1var itself is unallocated (not the memory it's pointing to). The allocated memory is not a variable. So when you exit f1, you have no way to access your allocated memory any more, as you've lost the address. BTW you've not explained why you need new addresses in your previous question.

Ilya
  • 5,377
  • 2
  • 18
  • 33
3

No, malloc() will not release the memory when the function terminates.

If I may offer a suggestion though ...

If you want to allocate space (for a (very) small object) only throughout a (non-recursive) function you can use C99's variable length arrays

int foobar(size_t small_n) {
    struct whatever autorelease[small_n]; // C99 VLA
    // use autorelease
    // no need to free
    return 0;
} // memory for autorelease object released
pmg
  • 106,608
  • 13
  • 126
  • 198
2

No, you have to use free (or realloc)

From http://www.cplusplus.com/reference/cstdlib/malloc/:

If the function reuses the same unit of storage released by a deallocation function (such as free or realloc), the functions are synchronized in such a way that the deallocation happens entirely before the next allocation.

More information: How do malloc() and free() work?

Community
  • 1
  • 1
CoderPi
  • 12,985
  • 4
  • 34
  • 62
2

In addition to other answers explaining about C dynamic memory allocation and the need to explicitly call free for each malloc-ed data (and when to call free is an issue; you want to avoid memory leaks), and you'll find a tool like valgrind helpful, you could also consider using Boehm's garbage collector. You basically could replace malloc by GC_malloc in your entire program and not bother a lot about explicit free-ing.

Of course, read the wikipage on garbage collection, and later the GC handbook. It does have some caveats. So Boehm's GC is not a silver bullet.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547