Is it possible to deallocate the memory of a null pointer?

This depends on what you mean by de-allocate the memory of a null pointer.

If you mean can you safely call delete (or delete []) on a null pointer the answer is yes for the default delete and delete [] operators. It should be yes for user defined versions of the delete operators if they have been written correctly.

Thus if the delete operator is correctly implemented:

   int * pi(0);   // Null pointer to int.
   delete pi;     // OK, delete checks for null pointers.

As mentioned in the comment next to the delete statement above the reason you can call delete on a null pointer is that delete checks (or should check) if the pointer is null before trying to use it. A null pointer is a pointer that notionally points nowhere and therefore points at no allocated memory and so trying to de-allocate what is not allocated is not a good idea. See Scott Meyers excellent book "Effective C++", item 8 in particular for this question but the rest of the book is full of relevant points for C++ programmers.

Of course if you mean can you de-allocate the memory for the pointer itself, then the answer is yes, if that memory were dynamically allocated of course, thus:

   int ** ppi = new int*(0); // create new pointer to int initialised
         // to the null pointer value.

   delete ppi;          // OK, ppi is a non-null pointer to a
         // pointer to int (otherwise new would
         // have thrown a std::bad_alloc
         // exception, assuming standard C++
         // behaviour).  


All Answers

Answers by Expert:

Ask Experts


Ralph McArdell


I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.


My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.


©2016 About.com. All rights reserved.