Does free(ptr) where ptr is NULL corrupt memory?

CPointersMemory ManagementNullFree

C Problem Overview


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?

C Solutions


Solution 1 - C

> 7.20.3.2 The free function

> Synopsis

> #include void free(void *ptr);

> Description

> The free function causes the space pointed to by ptr to be deallocated, that is, made available for further allocation. If ptr 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.

Solution 2 - C

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);

Solution 3 - C

> If ptr is NULL, no operation is performed.

says the documentation.

Solution 4 - C

I remember working on PalmOS where free(NULL) crashed.

Solution 5 - C

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.

Solution 6 - C

Recomended usage:

free(ptr);
ptr = NULL;

See:

man free

     The 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.

Solution 7 - C

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.

Solution 8 - C

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.

Solution 9 - C

not memory corruption, but behavior depends on implementation. By standard, it should be a legal code.

Solution 10 - C

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVijayView Question on Stackoverflow
Solution 1 - CGregory PakoszView Answer on Stackoverflow
Solution 2 - CR Samuel KlatchkoView Answer on Stackoverflow
Solution 3 - CMichael Krelin - hackerView Answer on Stackoverflow
Solution 4 - CjlruView Answer on Stackoverflow
Solution 5 - CPrasoon SauravView Answer on Stackoverflow
Solution 6 - CstefanBView Answer on Stackoverflow
Solution 7 - Cn0rdView Answer on Stackoverflow
Solution 8 - Candi8086View Answer on Stackoverflow
Solution 9 - CPavel RadzivilovskyView Answer on Stackoverflow
Solution 10 - CkishanpView Answer on Stackoverflow