157

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?

8
  • 1
    not sure about C free standard, but in C++ delete(NULL) is perfectly valid, so I guess free(NULL) should also be. Commented Dec 21, 2009 at 7:47
  • 16
    @Pryank: delete NULL is not valid in C++. delete can be applied to null-pointer values of concrete type, but not to NULL. delete (int*) NULL is legal, but not delete NULL. Commented Dec 21, 2009 at 7:53
  • 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? Commented Dec 21, 2009 at 8:04
  • 4
    No. If ptr points to memory, and you don't call free on it, then the memory will leak. Setting it to NULL just loses your handle on the memory, and leaks. If the ptr happens to be NULL, calling free is a no-operations. Commented Dec 21, 2009 at 8:05
  • 2
    @benjamin: Huh? What made you to conclude that you can replace free(ptr) with ptr = NULL. No one said anything like that. Commented Dec 21, 2009 at 8:07

11 Answers 11

271

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.

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.

Sign up to request clarification or add additional context in comments.

10 Comments

No, ptr=NULL is no way a replacement for free(ptr), both are completely different
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()
ptr=NULL ensures that even if you accidently call free(ptr) your program won't segfault.
You don't need to ask your question multiple times. Edit your question post and append the information you want.
@WereWolfBoy he means avoid free(NULL) by testing the pointer against NULL before calling free()
|
35

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

9 Comments

-1 [citation needed]. Changing code-style because of some theory of an archaic hearsay implementation is a bad idea.
@Tomas - I never recommended changing style, I simply explained why you may still see this recommendation in some styles.
@Tomas 3BSD (winehq.org/pipermail/wine-patches/2006-October/031544.html) and PalmOS for two (2nd hand for both).
@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.
Netware crashes on free-ing NULL too... (just debugged a crash on it...)
|
16

If ptr is NULL, no operation is performed.

says the documentation.

2 Comments

do u mean taht free will not perform anything?
benjamin, that's exactly what it means. What would you expect it to perform if it's aware of nullness of the argument?
13

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

3 Comments

Interesting - that makes a second platform (after 3BSD) that crashes.
If I remember correctly, on Palm the C Standard Library didn't exist. Instead, there was a mostly unsupported header file that mapped standard library calls through to the Palm OS SDK. Lots of things acted unexpectedly. Crashing on NULL was one of the big running differences of the Palm toolbox compared to the standard library.
PalmOS was freestanding C implementation and therefore had no obligation to provide the standard C library. Its analogue to free (MemPtrFree) was not standards compliant, and free was aliased to MemPtrFree as a (crude) attempt to provide a standard-like API.
10
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.

Comments

8

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.

1 Comment

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

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.

6 Comments

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).
You cannot delete void* either :P Which destructors(s) should it run?
@GMan: You can delete void * as long as it is a null-pointer.
Ok, fair enough. I forgot we're only dealing specifically with null.
usually does not corrupt anything, but is not guaranteed to. ASLR makes this rather unlikely, but still not impossible: buf1=malloc(X); free(buf1);buf2=malloc(X);free(buf1); - here if you're unlucky, buf2 got the exact same address as buf1, and you accidentally freed buf1 twice, so on the 2nd free of buf1 you actually freed buf2 silently, without casuing any (immidate) error/crash/whatever. (but you'll still probably get a crash next time you try to use buf2 - and this scenario is very unlikely if you're running on ASLR)
|
5

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.

2 Comments

Interesting fact, caught me by surprise. Made me feel compelled to take a trip around NULL pointer questions/answers.
However you will never free this port.
1

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

Comments

0

Although it is safe nowadays, I always use the following macro to free pointers:

#define FREE(ptr)      \ 
{                      \
    if ((ptr) != NULL) \
    {                  \
        free(ptr);     \
        (ptr) = NULL;  \
    }                  \
}

Comments

-5

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.

2 Comments

But if the memory is already freed as in the question then the pointer is invalid and should be set to NULL so if it is freed again there will not be a double free error
Order matters. free(ptr); ptr=NULL; is extremely different from ptr=NULL; free(ptr); The first deallocates the memory (variable gets its read/write rights to that chunk of memory revoked) and then removes any way of accessing that memory, which is completely safe and fine (as long as you never assign anything to ptr without also allocating new memory to ptr in the future). The second removes any way of accessing that memory (ptr=NULL;), then tries to access it in order to revoke its rights (via free(ptr))!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.