freeing a null pointer

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?

Answers


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.


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

If ptr is NULL, no operation is performed.


freeing null pointer will have no effect in the execution .


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.


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

;-||


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


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.


Need Your Help

Populated Table View won't display data

xcode uitableview populate

I'm a novice programmer working through "The Complete Idiot's Guide to iPad &amp; iPhone App Development" by Troy Brant (my problem is in Chapter 14, the "Frenemy" app). At the time of publication ...

Issue while converting HTML to JSON

jquery html json

I have a div which contains this:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.