views:

178

answers:

4

I'm trying to free up the memory I've allocated with malloc, but the free command doesn't seem to do its job properly according to Eclipse's debugger. How's this possible?

Below is a screenshot of my debugger after it supposedly freed up seCurrent->student->year, which is clearly not the case. year was allocated using malloc.

alt text

+10  A: 

free() does not normally change any values in your program - it just makes adjustments to the C runtime heap. This means that the values in the memory that was just freed are retained. However, attempts to access them from your code lead to undefined behaviour.

anon
Would you recommend `NULL`-ing the pointer after freeing it?
Pieter
@Pieter Set it to something invalid, recognizable by a human as invalid, and not accepted by `free`. The standard says that `free` is supposed to accept `NULL`, so you may hide a double-freeing-bug if you set the pointer to NULL.
Pascal Cuoq
On the other hand, if you set the pointer to NULL and then free it again, is that really a bug?
bk1e
@bk1e: freeing it again isn't a bug, but if your program does it and you (the programmer) didn't realise it was going to, then that means you don't understand your own program. This situation is very strongly correlated with the program containing bugs :-) Of course it's possible to concoct situations where you know that the program may or may not free the null, and don't care which, but Pascal is talking about cases where you double-free accidentally. NULLing the pointer prevented undefined behaviour, but just because you dodged a bullet doesn't mean your bug only fired one.
Steve Jessop
This has been debated a few times on SO, though. Summary as far as I can tell: a policy of NULLing all freed pointers has some advantages. Not NULLing them unless NULL means something in that context has some advantages. Some people feel fairly strongly that one set of advantages outweighs the other.
Steve Jessop
+5  A: 

What makes you think it hasn't freed it? Freeing memory means that accessing it from the program thereafter is undefined behavior, and the memory is available for re-use next time you call malloc. It does not promise to overwrite the data that was stored in the memory you freed, or to prevent the debugger from reading the unallocated memory.

Steve Jessop
+4  A: 

Free will return the allocated space to the heap to be reused by subsequent mallocs but it does not change the values of any pointers that previously referenced that memory. In your case, no other mallocs have yet been performed so the memory just freed is still the same as it was just prior to the call to free. In order for your code to know that there is no longer any data associated with the pointer, you may want to set it to null after freeing the memory associated with it.

tvanfosson
A: 

when you malloc() some memory, all it does is searching for some free space in memory, and keeping track it is now used. It doesn't initialize it or whatever.

when you call free(), all it does is clearing this memory block out of the list of used memory blocks. Again it doesn't modify the contents.

f4