No, you don't "free the pointer", you free the memory the pointer is pointing at.
This should be pretty easy to understand, if you just think about it some more. Consider code like this, which is meant to be a simplified view of what you're asking:
void *ptr = malloc(1024); /* Allocate 1024 KB of memory, somewhere. */
void *copy1 = ptr;
void *copy2 = ptr;
void *copy3 = ptr;
void *copy4 = ptr;
When the above has run, assuming the allocation succeeds, we clearly have five pointers to the same block of memory. We can de-allocate the memory using any of the five pointers, since their values are identical:
free(ptr3); /* Any one would work. */
Of course, we can't call free()
with more than one of the pointers, since all the pointers are pointing at the same block of memory.
You can only free a block of memory once, it's undefined behavior to call free()
multiple times for the same address (unless you did a new allocation in-between of course).
This is of course explained in the manual page which you really should study:
The free()
function frees the memory space pointed to by ptr, which must have been returned by a previous call to malloc()
, calloc()
or realloc()
. Otherwise, or if free(ptr)
has already been called before, undefined behavior occurs. If ptr
is NULL
, no operation is performed.