Couldn't seem to find an answer to my question elsewhere.
Consider the code:
int *ptr = malloc (sizeof (*ptr));
int *dummyPtr = ptr;
free (dummyPtr);
printf ("ptr: %p, dummy: %p\n", ptr, dummyPtr);
As I understand it, free() takes in a pointer and makes it point to NULL aka (nil). I.e., the pointer is dereferenced and points nowhere. This means that the memory pointed to by the pointer is effectively erased.
Two parts to this question:
- If I run the above code,
dummyPtrstill seems to point to an address in memory and not(nil). Why? - What happens to
ptr? Will it still point to that block of memory or has it been dereferenced too?
I've run this code and some variants a few times with conflicting results. Sometimes only the copy is affected, sometimes both. Sometimes the values pointed to are even set to zero!
Frankly, I have no idea what's going on. (I'm running Debian, if that makes a difference.)