The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Freeing memory from heap
Discuss Freeing memory from heap in the C Programming forum on Dev Shed. Freeing memory from heap C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 5th, 2013, 06:08 AM
|
|
Contributing User
|
|
Join Date: Nov 2012
Posts: 111
Time spent in forums: 1 Day 4 h 5 m 15 sec
Reputation Power: 1
|
|
|
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!
|

January 5th, 2013, 06:48 AM
|
 |
Contributed User
|
|
|
|
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);
|

January 5th, 2013, 06:49 AM
|
 |
Contributing User
|
|
Join Date: Aug 2003
Location: UK
|
|
Quote: | 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()
Quote: | 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().
|

January 5th, 2013, 07:38 AM
|
|
Contributing User
|
|
Join Date: Nov 2012
Posts: 111
Time spent in forums: 1 Day 4 h 5 m 15 sec
Reputation Power: 1
|
|
OK, thanks.
and what happen if i didn't free it and ran the program like hundred times already? 
Last edited by so.very.tired : January 5th, 2013 at 07:40 AM.
|

January 5th, 2013, 09:44 AM
|
 |
Contributing User
|
|
Join Date: Aug 2003
Location: UK
|
|
Quote: | Originally Posted by so.very.tired
and what happen if i didn't free it and ran the program like hundred times already?  | 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 09:49 AM.
|

January 5th, 2013, 02:54 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
Quote: | 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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|