tags:

views:

491

answers:

7

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?

+13  A: 

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

If ptr is NULL, no operation is performed.

Martin B
For those who fears it is a Linux extension, that behavior is mandated by C90 standard.
AProgrammer
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).
AndreyT
+2  A: 

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
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
My point was stated clearly. The "warning" (only useful to the programmer) would interfere with the normal program output.
Daniel Daranas
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
+22  A: 

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
Why a downvote?
Prasoon Saurav
A: 

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
Access to shared data in multithreaded applications should be guarded, which will prevent the memory leak you describe.
outis
@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
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.
Roger Pate
@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
+3  A: 

freeing null pointer will have no effect in the execution .

pavun_cool
A: 

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
first of all you probably mean "safe_free". but what would be the point of this. `free(NULL)` is perfectly legal. There is no point in checking for `NULL` before `free`ing it.
Evan Teran
Can't you recognize a joke (even when spelled badly)?
edgar.holleis
Jokes apart, the Kamailio-SIP-Server http://www.kamailio.org/, with warnings enabled, logs a warning when you free(0).
edgar.holleis
+2  A: 

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