Until today I lived in belief that calling free()
on memory space releases it for further allocation without any other modifications. Especially, considering th
There is another pitfall you might have not known actually, here:
free(pointer);
printf("After free(): %p \n", pointer);
Even just reading the value of pointer
after you free
it is undefined behaviour, because the pointer becomes indeterminate.
Of course dereferencing the freed pointer - like in below example - is also not allowed:
free(pointer);
printf("After free(): %p, %d\n", pointer, *pointer);
ps. In general when printing address with %p
(like in printf
) cast it to (void*)
, e.g. (void*)pointer
- otherwise you get undefined behaviour also