views:

717

answers:

8

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?

A: 

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

Pavel Radzivilovsky
+4  A: 

If ptr is NULL, no operation is performed.

says the documentation.

Michael Krelin - hacker
do u mean taht free will not perform anything?
Vijay Sarathi
benjamin, that's exactly what it means. What would you expect it to perform if it's aware of nullness of the argument?
Michael Krelin - hacker
+2  A: 

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.

n0rd
`delete 0` is not legal in C++. `delete` explicitly requires an expression of pointer type. It is legal to apply `delete` to a typed null-pointer value, but not to `0` (and not to `NULL`).
AndreyT
Yup, fixed snippets.
n0rd
You cannot delete `void*` either :P Which destructors(s) should it run?
GMan
@GMan: You *can* delete `void *` as long as it is a null-pointer.
AndreyT
Ok, fair enough. I forgot we're only dealing specifically with null.
GMan
+7  A: 
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.

Prasoon Saurav
+37  A: 

7.20.3.2 The free function

Synopsis

#include <stdlib.h> 
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.

Gregory Pakosz
+1 for Synopsis.
Prasoon Saurav
so it means if a pointer is pointing to NULL free does not perform anything.does that mean!!!!!! every time in our coding if want to free a memory can simply replace a free(ptr) with ptr=NULL?
Vijay Sarathi
No, ptr=NULL is no way a replacement for free(ptr), both are completely different
novice_coder
NO, it means `free(ptr)` where `ptr` is null has no side effects. But in any case, every memory allocated using `malloc()` or `calloc()` must be released afterwards using `free()`
Gregory Pakosz
ptr=NULL ensures that even if you accidently call free(ptr) your program won't segfault.
novice_coder
You don't need to ask your question multiple times. Edit your question post and append the information you want.
GMan
+2  A: 

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);
R Samuel Klatchko
-1 [citation needed]. Changing code-style because of some theory of an archaic hearsay implementation is a bad idea.
Tomas
@Tomas - I never recommended changing style, I simply explained why you may still see this recommendation in some styles.
R Samuel Klatchko
@Tomas 3BSD (http://www.winehq.org/pipermail/wine-patches/2006-October/031544.html) and PalmOS for two (2nd hand for both).
Douglas Leeder
@Tomas: the problem was in things like Version 7 Unix. When I was learning, free(xyz) where xyz == NULL was a recipe for instant disaster on the machine where I learned (ICL Perq running PNX, which was based on Version 7 Unix with some System III extras). But I've not code that way for a long time.
Jonathan Leffler
+1  A: 

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.

stefanB
That also help to spot segfaults with a debugger. It is evident that segfault at p->do() with p=0 is someone using a freed pointer. Less evident when you see p=0xbfade12 in debugger :)
neuro
+2  A: 

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

jlru
Interesting - that makes a second platform (after 3BSD) that crashes.
Douglas Leeder