Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens to memory after free()?

I know that on your hard drive, if you delete a file, the data is not (instantly) gone. The data is still there until it is overwritten. I was wondering if a similar concept existed in memory. Say I allocate 256 bytes for a string, is that string still floating in memory somewhere after I free() it until it is overwritten?

like image 724
Lienau Avatar asked Feb 26 '11 04:02

Lienau


2 Answers

Your analogy is correct. The data in memory doesn't disappear or anything like that; the values may indeed still be there after a free(), though attempting to read from freed memory is undefined behaviour.

like image 118
chrisaycock Avatar answered Oct 11 '22 17:10

chrisaycock


Generally, it does stay around, unless you explicitly overwrite the string before freeing it (like people sometimes do with passwords). Some library implementations automatically overwrite deallocated memory to catch accesses to it, but that is not done in release mode.

like image 29
Jeremiah Willcock Avatar answered Oct 11 '22 16:10

Jeremiah Willcock