104

Possible Duplicate:
Does free(ptr) where ptr is NULL corrupt memory?

I'm writing a C function that frees a pointer if it was malloc()ed. The pointer can either be NULL (in the case that an error occured and the code didn't get the chance to allocate anything) or allocated with malloc(). Is it safe to use free(ptr); instead of if (ptr != NULL) free(ptr);?

gcc doesn't complain at all, even with -Wall -Wextra -ansi -pedantic, but is it good practice?

Community
  • 1
  • 1
rid
  • 61,078
  • 31
  • 152
  • 193
  • 1
    see also: [checking for null before calling free](http://stackoverflow.com/questions/1912325/checking-for-null-before-calling-free) – Mat May 21 '11 at 20:25
  • Reopen. The question was, ***"... is it good practice"***; and not *:...is it legal"*. They are two different questions. I am interested to know the justification for freeing a NULL pointer since nothing can be free'd. In my mind's eye it makes no sense and it is a program bug. – jww Dec 22 '17 at 14:22

4 Answers4

195

Quoting the C standard, 7.20.3.2/2 from ISO-IEC 9899:

void free(void *ptr);

If ptr is a null pointer, no action occurs.

Don't check for NULL, it only adds more dummy code to read and is thus a bad practice.


However, you must always check for NULL pointers when using malloc & co. In that case NULL mean that something went wrong, most likely that no memory was available.

orlp
  • 112,504
  • 36
  • 218
  • 315
  • I am. I have a structure containing pointers. I try to allocate each of them. The first time an allocation fails (the pointer returned by `malloc()` is NULL), I call the deallocation function, which goes through all of them and frees the ones that were allocated. Then I signal the error. Is there any better way to do this? – rid May 22 '11 at 00:12
  • 1
    @rdineiu, assuming you initialize the pointers to null before the `malloc` calls (so you never `free` a uninitialized pointer), that's exactly the right way to do that. – David X May 22 '11 at 07:33
  • 3
    Some answers on this StackOverflow link: http://stackoverflow.com/questions/1938735/does-freeptr-where-ptr-is-null-corrupt-memory mention that certain C libraries ignore this standard, and throw errors, in spite of what the C standard says. Might be worth looking at. – Abraham Philip Jan 14 '15 at 17:56
  • 1
    "malloc & co." you mean calloc() – jasonleonhard Feb 20 '21 at 02:16
31

It is good practice to not bother checking for NULL before calling free. Checking just adds unnecessary clutter to your code, and free(NULL) is guaranteed to be safe. From section 7.20.3.2/2 of the C99 standard:

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.

As noted in the comments, some people sometimes wonder if checking for NULL is more efficient than making a possibly unnecessary function call. However, this:

  • Is a premature micro-optimization.
  • Shouldn't matter. Checking for NULL first even might be a pessimization. For example, if 99% of the time your pointers aren't NULL, then there would be a redundant NULL check 99% of the time to avoid an extra function call 1% of the time.
jamesdlin
  • 81,374
  • 13
  • 159
  • 204
  • What if the call to `free` has overhead. Would it not be more efficient to avoid the call? – jww Dec 22 '17 at 14:23
  • 1
    @jww It could be, but you have to ask yourself if the tradeoff is worth it. The *common* case is that things don't fail and that you probably end up with allocated memory instead of null pointers. If you add null-pointer checks everywhere, this means that you've now negatively impacted the performance of your common cases (and added clutter to your code) just to avoid an extra function call in what's probably an atypical failure case. – jamesdlin Dec 23 '17 at 01:28
  • @jww The overhead is logically no more than `if(ptr==NULL) return;' so all you can expect to achieve by checking yourself is doubling the overhead! – Persixty Jan 14 '18 at 12:15
  • 1
    @Persixty Not exactly since there's overhead from a function call itself. – jamesdlin Aug 06 '19 at 15:25
  • @jamesdin A fair point. A smart compiler could inline the check and only call when `free()` was needed. But I know of no real examples. So using a realistic factor that an impotent call to `free()` is 6x the check you win unless more than 5/6 of pointers are non-NULL. Of course in many applications the pointer is rarely non-NULL. So in practice you were right in Dec 2017. There's a trade-off. – Persixty Aug 12 '19 at 11:51
7

See http://linux.die.net/man/3/free which states:

If ptr is NULL, no operation is performed.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Hello71
  • 766
  • 8
  • 17
4

In my opinion, no, at least not in your case.

If you couldn't allocate memory, you should have checked that WAY before the call of free.

George
  • 3,765
  • 21
  • 25