Let us see the C11
standard, chapter §7.22.3.3.
The free
function causes the space pointed to by ptr
to be deallocated, that is, made
available for further allocation. [....]
So, that means, the pointer which was passed to free()
is no longer a "valid" pointer in your program context. Thereby, attempting to access the pointer, you're essentially accessing an invalid memory which causes undefined behavior.
Now, what actually happens to the memory location and to the content of the memory location, is depending on the memory manager or the OS. It could just be left as-is, unless it is required to be allocated again by some further calls, it could either be zeroed-out, anything...that is beyond the C standard specification.
Then, I'm not preaching for the coding style, but I personally find it handy to set any free-d pointer to NULL
immediately after free()
. "Most of the time", this will prevent any further use of the pointer (via a segfault), as NULL
is definitely an invalid pointer in all context.
That said, some points regarding your code snippet,