Theoretically I can say that
free(ptr);
free(ptr);
is a memory corruption since we are freeing the memory which has already been freed.
But what if
free(ptr);
ptr=NULL;
free(ptr);
As the OS will behave in an undefined manner I cannot get an actual theoretical analysis for this about what's happening. Whatever I am doing, is this memory corruption or not?
Is freeing a NULL pointer valid?
delete NULLis not valid in C++. delete can be applied to null-pointer values of concrete type, but not toNULL.delete (int*) NULLis legal, but notdelete NULL.ptrpoints to memory, and you don't callfreeon it, then the memory will leak. Setting it toNULLjust loses your handle on the memory, and leaks. If theptrhappens to beNULL, callingfreeis a no-operations.free(ptr)withptr = NULL. No one said anything like that.