7

Suppose I came across an instance in a program where I would either free a NULL pointer, or first check whether it was NULL and skip the free() function call.

Would it be more efficient to simply free a NULL pointer? I searched around a bit and apparently, for implementations post C89, it is harmless to free a NULL pointer - so the decision comes down to efficiency.

My presumption is that there may potentially entail quite a bit of overhead when calling free(). As such, perhaps the simple logical check before calling the free() function is quite necessary.


tl;dr version,

What is happening internally when a call to free() is made that may make it more or less efficient to first check whether or pointer is NULL before freeing?

sherrellbc
  • 4,650
  • 9
  • 48
  • 77
  • What? The tldr has nothing to do with your question! – Kerrek SB Sep 12 '13 at 22:56
  • Made it a bit more specific, but it was the overhead associated with calling `free()` that I was concerned with. – sherrellbc Sep 12 '13 at 23:02
  • free(NULL) is guaranteed to be a no-op since c89. BTW: don't be too obsessed by performance. The only difference is the NULL-check being done inside or outside the free() function. – wildplasser Sep 12 '13 at 23:05
  • 1
    @wildplasser: No, the difference is between the check being done inside or inside *and* outside. – Kerrek SB Sep 12 '13 at 23:07
  • No, if you do the check _before_ the function you won't call the function. (but the code is still there , inside the function which won't be called) I presume you consider it a kind of Monty-Hall scenario? – wildplasser Sep 12 '13 at 23:08
  • @wildplasser: In the presumably likely case that the pointer is not NULL, the check will be done twice. Clearly, if you are almost certain the pointer is NULL, it's faster to check before the call, but if you expect the pointer to be non-NULL it's foolish. – rici Sep 12 '13 at 23:52
  • 2
    Possible duplicate of [Does free(ptr) where ptr is NULL corrupt memory?](http://stackoverflow.com/questions/1938735/does-freeptr-where-ptr-is-null-corrupt-memory) – Matt Krause Jan 05 '16 at 09:50

3 Answers3

11

The C standard guarantees that calling free(NULL) is harmless and has no effect. So, unless you believe that calling free() on a NULL pointer indicates that you've got a logic error elsewhere in your program, there's no reason to double-check that.

This isn't my real name
  • 4,869
  • 3
  • 17
  • 30
8

Don't check for nullness yourself. free already has to do that anyway and is guaranteed to do nothing when called with a null pointer. Simple as that.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
4

The Open Group specification for free() states that:

If ptr is a null pointer, no action shall occur.

This implies that the implementation of free() will probably begin with something to the effect of:

if (ptr == NULL)
    return;

which will have very little overhead. No guarantees, but there isn't much work the function could do before it makes that check, so checking on your own is unnecessary.

  • I'll see your POSIX.1-2001 and raise you a [POSIX.1-2008](http://pubs.opengroup.org/onlinepubs/9699919799/). [`free()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/free.html). – This isn't my real name Sep 13 '13 at 02:01