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 NULL
is not valid in C++. delete can be applied to null-pointer values of concrete type, but not toNULL
.delete (int*) NULL
is legal, but notdelete NULL
. – Townshipptr
points to memory, and you don't callfree
on it, then the memory will leak. Setting it toNULL
just loses your handle on the memory, and leaks. If theptr
happens to beNULL
, callingfree
is a no-operations. – Myronfree(ptr)
withptr = NULL
. No one said anything like that. – Townshipfree( someIntPointer )
===> tellOperatingSystemThat_someIntPointer
_noLongerHasRightsToReadNorWrite_sizeOfInteger_AmountOfMemory.ptr = NULL;
means that the pointer is pointing to memory address 0x00000000 (there may be more hex 0s added to the end of that number, but it still has memory address 0). This statement is NOT saying what data lies at that address, nor how many bytes will be stored there (since no type) – Haynor