#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    132
    Rep Power
    3

    Freeing memory from heap


    do i have to use the free() to free memory after every memory allocation with malloc()?

    and if so, how do i free up a linked list?
    do i have to iterate through the whole list to free it, or just the head of the list is enough?

    thanks in advanced!
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,413
    Rep Power
    1871
    Each malloc() needs exactly ONE matching free().

    >and if so, how do i free up a linked list?
    Well that depends how you allocated it.

    If it was just a large block of memory with some internal pointers to make it look like a list, then you just have one free.

    If you allocated each node separately, then you need to traverse the list and free each node separately as well.
    Note that if you do this
    Code:
    free ( node );
    node = node->next;
    then the code is broken.
    You need
    Code:
    tmp = node;
    node = node->next;
    free(tmp);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,117
    Rep Power
    1803
    Originally Posted by so.very.tired
    do i have to use the free() to free memory after every memory allocation with malloc()?
    You should free it after your code will no longer reference it, not immediately after the malloc() ;)

    Originally Posted by so.very.tired
    and if so, how do i free up a linked list?
    That would depend entirely on your linked list implementation and how the objects in the list were allocated in the first instance. The language knows nothing about your implementation, so it cannot automatically free any memory indirectly referenced. Basically each individual malloc() must have a corresponding free().
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    132
    Rep Power
    3
    OK, thanks.


    and what happen if i didn't free it and ran the program like hundred times already? :eek:
    Last edited by so.very.tired; January 5th, 2013 at 08:40 AM.
  8. #5
  9. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,117
    Rep Power
    1803
    Originally Posted by so.very.tired
    and what happen if i didn't free it and ran the program like hundred times already? :eek:
    In most operating systems, the heap belonging to your process is returned to the system when the process terminates, so nothing bad.

    The problem occurs when your process continually makes allocations without freeing them, and/or looses the reference (perhaps by reassigning it or because it was a local variable, or perhaps you freed a list without freeing its content) so it can never free them. If your process then runs for long enough it will increase its memory usage indefinitely until it is exhausted. What happens then depends on the system. Typically in Windows you might first notice your system slowing down as all processes are starved of resources and disk swapping starts to occur almost constantly, in the worst case the system become so unresponsive you even have trouble closing down the errant process.

    In Windows you can monitor an individual process's memory usage in the task manager on the Processes tab, select View->Select Columns to select which resources to view.

    In Visual C++'s debugger, if a process terminates without cleaning up its resources, the process termination report will include a warning. In Linux you can use a tool called Valgrind to check for memory allocation errors.
    Last edited by clifford; January 5th, 2013 at 10:49 AM.
  10. #6
  11. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,253
    Rep Power
    2222
    Originally Posted by so.very.tired
    OK, thanks.


    and what happen if i didn't free it and ran the program like hundred times already? :eek:
    That's called a memory leak. There is a finite amount of heap and if you keep allocating from it without freeing up what you no longer need, then you will eventual use it all up. When that happens, your next malloc will fail your program will crash "unexpectedly and for no reason".

    Normally, you won't see this happen in your programming assignments because they run for such a short time. But professional programs are often required to run continuously for long periods of time so these are the ones that will fall victim to the memory leaks and will crash in the middle of the night "for no reason". When that program provides a vital service such as supplying power to a community or keeping a hospital patient alive, that can cause real problems.

    It is therefore important to develop the discipline of preventing memory leaks. A classic memory leak happens when you drop a pointer. Since every block of memory allocated from the heap must be pointed to at all times, when that no longer happens then you have dropped that pointer and you can no longer free that memory. You can drop a pointer in a variety of ways, such as malloc'ing with a local pointer and then returning from the function before saving it, free'ing a linked list node before saving the pointer to the next node, assigning a different address to a pointer without having saved the first address. Needless to say, working with a data structure such as a linked list provides you with plenty of opportunities to drop a pointer.

    Finding and correcting a memory leak can be very difficult, especially in a commercial-grade program with many tens of source files, so the best approach is to prevent them in the first place.

IMN logo majestic logo threadwatch logo seochat tools logo