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?
Best Answer
7.20.3.2 The
free
functionSynopsis
#include <stdlib.h> void free(void *ptr);
Description
The
free
function causes the space pointed to byptr
to be deallocated, that is, made available for further allocation. Ifptr
is a null pointer, no action occurs.
See ISO-IEC 9899.
That being said, when looking at different codebases in the wild, you'll notice people sometimes do:
if (ptr)free(ptr);
This is because some C runtimes (I for sure remember it was the case on PalmOS) would crash when freeing a NULL
pointer.
But nowadays, I believe it's safe to assume free(NULL)
is a nop as per instructed by the standard.
All standards compliant versions of the C library treat free(NULL) as a no-op.
That said, at one time there were some versions of free that would crash on free(NULL) which is why you may see some defensive programming techniques recommend:
if (ptr != NULL)free(ptr);
If ptr is NULL, no operation is performed.
says the documentation.
I remember working on PalmOS where free(NULL)
crashed.
free(ptr);ptr=NULL;free(ptr);/*This is perfectly safe */
You can safely delete a NULL pointer. No operation will be performed in that case.In other words free() does nothing on a NULL pointer.
Recomended usage:
free(ptr);ptr = NULL;
See:
man freeThe free() function deallocates the memory allocation pointed to by ptr.If ptr is a NULL pointer, no operation is performed.
When you set the pointer to NULL
after free()
you can call free()
on it again and no operation will be performed.
free(NULL)
is perfectly legal in C as well as delete (void *)0
and delete[] (void *)0
are legal in C++.
BTW, freeing memory twice usually causes some kind of runtime error, so it does not corrupt anything.
free(ptr)
is save in C if ptr
is NULL
, however, what most people don't know is that NULL
need not be equal to 0. I have a nice old-school example: On the C64, on address 0, there is an IO-Port. If you wrote a program in C accessing this port, you'd need a pointer whose value is 0. The corresponding C library would have to distinguish between 0 and NULL
then.
Kind regards.
not memory corruption, but behavior depends on implementation.By standard, it should be a legal code.
Although it is safe nowadays, I always use the following macro to free pointers:
#define FREE(ptr) \ { \if ((ptr) != NULL) \{ \free(ptr); \(ptr) = NULL; \} \}
ptr is pointing to some memory location, lets say 0x100.
When you free(ptr), basically you are allowing 0x100 to be used by memory manager to be used for other activity or process and in simple words it is deallocation of resources.
When you do ptr=NULL, you are making ptr point to new location(lets not worry about what NULL is). Doing this you lost track of the 0x100 memory data.This is what is memory leak.
So it is not advisable to use ptr=NULL on a valid ptr.
Instead you could do some safe check by using :
if(ptr != NULL){free(ptr);}
When you free(ptr) where ptr is already pointing to NULL, it performs no operation.So, its safe to do so.