Does Free () not free memory properly?

I am trying to free the memory that I allocated using malloc , but the free command does not work properly according to the Eclipse debugger. How is this possible?

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

alt text http://img693.imageshack.us/img693/7840/codeo.png

+4
source share
4 answers

free () usually does not change any values ​​in your program - it just adjusts a bunch of C runtimes. This means that the values ​​in the memory that was just freed are saved. However, attempts to access them from your code result in undefined behavior.

+12
source

Why do you think that he did not set him free? Free memory means that after that access to it from the program will be undefined, and the memory will be available for reuse the next time malloc is called. It does not promise to overwrite data stored in freed memory, or to prevent the debugger from reading unallocated memory.

+7
source

Free will return the allocated space to the heap, which will be reused by subsequent mallocs, but it will not change the value of any pointers that previously referred to this memory. In your case, no other mallocs have yet been executed, so the freed memory is still the same as before the free call. So that your code knows that there is no more data associated with the pointer, you can set it to null after releasing the memory associated with it.

+5
source

when you malloc() some memory, all it does is search for some free space in memory, and tracking is now used. He does not initialize it or anything else.

when you call free() , all it does is free this block of memory from the list of used memory blocks. Again, it does not change the contents.

0
source

Source: https://habr.com/ru/post/1301949/


All Articles