Using memory that has already been free()
-d invokes undefined behavior. Don't do that.
Quoting C11
, annex §J.2, Undefined behavior
The value of a pointer that refers to space deallocated by a call to the free
or
realloc
function is used.
To elaborate, calling free()
on a pointer does not set the pointer to NULL
or anything else. It just marks the memory as re-usable by the memory manager. That memory location (pointer) is not valid anymore for the program, so you should not be using it.
As a preventive measure to the above problem, it's considered a good coding practice to set the pointer to NULL
explicitly after calling free()
. However, FWIW, this is not mandatory.
Quoting C11
, 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. [...]
Also, just to add, partial free()
is not possible by calling free()
, anyway. You can refer to this previous answer for clarification.