35

What happens inside memory if we try to free a pointer which is pointing to NULL? Is that ever valid?

Why does it not show any warning/error messages?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Vijay
  • 65,327
  • 90
  • 227
  • 319
  • nothing; ...still, behavior of freeing a pointer that is non-null anything that wasn't malloc'd earlier is undefined and usually disastrous. – SF. Mar 01 '10 at 11:48
  • 9
    Pointers are not "pointing to NULL". Pointers either *are* or *are not* null themselves. "Pointer which is pointing to NULL" is a nonsensical statement. – AnT stands with Russia Mar 02 '10 at 02:18
  • if a pointer is storing an address of a varibale,it is a usual practice that a programmer will say pointer is pointing to that variable.so it is not a nonsensical statment.may be i am wrong.but it does not seem to me as a non sensical statement. – Vijay Mar 02 '10 at 04:21
  • 7
    When a pointer is null, it is not "storing the address of NULL". NULL has no address. When a pointer is null, it is simply not pointing anywhere at all. – AnT stands with Russia Mar 02 '10 at 05:27
  • This question is a duplicate of http://stackoverflow.com/questions/1912325/checking-for-null-before-calling-free – vog Dec 23 '10 at 14:05

7 Answers7

59

From C99 section 7.20.3.2 : The free function

Synopsis

 1 #include <stdlib.h>
   void free(void *ptr);

Description

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

Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345
15

From http://linux.die.net/man/3/malloc:

If ptr is NULL, no operation is performed.

Martin B
  • 23,670
  • 6
  • 53
  • 72
  • 12
    For those who fears it is a Linux extension, that behavior is mandated by C90 standard. – AProgrammer Mar 01 '10 at 08:56
  • It is interesting to note that the people who wrote that Linux man page either didn't understand or didn't care about the difference between `NULL` and "null pointer value". Most serious and/or formal documents are normally pretty pedantic about it (see other answers). – AnT stands with Russia Mar 02 '10 at 06:47
5

I'd go for:

#ifdef MY_DOUBTS_HAUNT_ME_IN_MY_DREAMS
    #define safe_free(x) do { if ((x)) free((x)); } while(0)
#elseif /* feeling gutsy */
    #define safe_free(x) free((x))
#endif

;-||

edgar.holleis
  • 4,803
  • 2
  • 23
  • 27
5

Once upon a very long time ago, there were implementations of 'free()' that crashed when given a null pointer to free. This only applies to implementations that pre-date the C89 (C90) standard that have not been modified to deal with the problem since.

In my experience, there are essentially none of those implementations left (and nor should there be), so it is now safe to free null pointers.

If you have any requirements to port to extremely weird and ancient systems, then maybe you should still be cautious. On the other hand, if you had such systems to worry about, you'd probably know about the issue (and a whole raft of other issues) - or there'd be some communal knowledge around the code that indicates this.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
4

freeing null pointer will have no effect in the execution .

Pavunkumar
  • 5,147
  • 14
  • 43
  • 69
2

What happens inside memory if we try to free a pointer which is pointing to NULL. is that ever valid?

Nothing.

why does it not show any warning/error messages?

First, the behaviour is valid by definition, so no error or warning needs to be issued.

Second, a pointer is pointing to NULL at runtime. How should a warning or error message be displayed, if at all? Imagine that you are playing a game called Kill the Zombie and while two of these beings are attacking you, a popup error message appears, saying: "Warning, NULL pointer freed."

Daniel Daranas
  • 22,454
  • 9
  • 63
  • 116
  • What's your point there? The example reminds me from windows and has nothing to do with logging error messages. The sensible decision to warnings is to output them into stderr. – Cheery Mar 01 '10 at 09:14
  • 3
    My point was stated clearly. The "warning" (only useful to the programmer) would interfere with the normal program output. – Daniel Daranas Mar 01 '10 at 09:36
  • Oh, he asked why does it not *show*. :) My bad. It's a weird question, almost nothing in C shows any warnings or errors without programmer checking the thing for errors. – Cheery Mar 02 '10 at 20:05
1

It might be safe (I didn't know that, but the other answers seem to suggest that), but I won't fall into the habit of not caring about whether the pointer is already null. The assignment p = NULL; after every free soon follows as a corollary. This is dangerous in multithreaded applications, as after this assignment, p might be used by another thread and would be freed again by the current thread while it is expected to be alive by the other threads.

Every malloc'd memory should be freed once. Period.

amit kumar
  • 20,438
  • 23
  • 90
  • 126
  • 1
    Access to shared data in multithreaded applications should be guarded, which will prevent the memory leak you describe. – outis Mar 01 '10 at 09:07
  • @outis The problem is not memory leak but deletion of data that the other threads expect is alive (probably will result in segmentation fault). Guarded access is not sufficient in this case. – amit kumar Mar 01 '10 at 10:50
  • What you mention about threading appears correct, but, as far as I can tell, doesn't apply to this question and you don't attempt to answer this question. –  Mar 03 '10 at 05:43
  • @Roger It definitely "applies to the question" since it talks about a bad habit to get into. Making the same points the others have made is not worthwhile, and others have already "correctly" answered the question. – amit kumar Mar 03 '10 at 10:31