p contains a copy of the (supposedly valid) address, so free (p) tries, well, to free it. But since this is a copy, she cannot really set her free.
It is not true. free() may work fine if p is a valid address returned by malloc() (or NULL ).
In fact, this is a general template for implementing user-defined "destructor" functions (when writing OO-style code in C).
What you probably mean is that p will not change to NULL after that, but this is natural since you pass it by value. If you want free() and reject the pointer, then pass it with the pointer ("byref"):
void func(int **p) { if (p != NULL) { free(*p); *p = NULL; } }
and use it like
int *p = someConstructor(); func(&p);
user529758
source share