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?
See ISO-IEC 9899.
That being said, when looking at different codebases in the wild, you’ll notice people sometimes do:
This is because some C runtimes (I for sure remember it was the case on PalmOS) would crash when freeing a
NULLpointer.But nowadays, I believe it’s safe to assume
free(NULL)is a nop as per instructed by the standard.