Take the 2-minute tour ×

Is it safe to delete a NULL pointer?

And is it a good coding style?

share|improve this question
See too stackoverflow.com/questions/724688/… –  Feral Oink Aug 25 '13 at 23:01
add comment

6 Answers

up vote 69 down vote accepted

delete performs the check anyway, so checking it on your side adds overhead and looks uglier. A very good practice is setting the pointer to NULL after delete (helps avoiding double deletion and other similar memory corruption problems).

I'd also love if delete by default was setting the parameter to NULL like in

#define my_delete(x) {delete x; x = NULL;}

(I know about R and L values, but wouldn't it be nice?)

share|improve this answer
Note that there still can be several other pointers pointing to the same object even if you set one to NULL on deletion. –  sth Nov 16 '10 at 2:47
In most cases in my code, the pointer goes out of scope once it's been deleted. Much safer than merely setting it to NULL. –  jalf Nov 16 '10 at 4:03
what about them? They're typically smart pointers, or they're members of my own ad hoc RAII class, which, like I said, goes out of scope when the pointer is deleted -- or they point to an object whose lifetime extends past that of the object containing the pointer. –  jalf Nov 16 '10 at 5:42
@ruslik: raw pointer members are an extremely bad idea in modern C++. –  FredOverflow Nov 19 '10 at 17:27
A very god practice is not setting the pointer to NULL after delete. Setting a pointer to NULL after deleting it masquerades memory allocation errors, which is a very bad thing. A program that is correct does not delete a pointer twice, and a program that does delete a pointer twice should crash. –  Damon Aug 30 '13 at 18:48
show 3 more comments

Yes it is safe.

There's no harm in deleting a null pointer; it often reduces the number of tests at the tail of a function if the unallocated pointers are initialized to zero and then simply deleted.

share|improve this answer
add comment

Good practice is to write C++ programs without a single call to delete. Use RAII instead.

That is, use std::vector<T> v(100); instead of T* p = new T[100];, use smart pointers like unique_ptr<T> and shared_ptr<T> that take care of deletion instead of raw pointers etc.

share|improve this answer
add comment

Deleting a null pointer has no effect. It's not good coding style necessarily because it's not needed, but it's not bad either.

If you are searching for good coding practices consider using smart pointers instead so then you don't need to delete at all.

share|improve this answer
the time people want to delete a NULL pointer is when they're not sure if it contains NULL... if they knew it was NULL then they wouldn't be considering delete and hence asking ;-). –  Tony D Nov 16 '10 at 4:44
@Tony: My point was only that it will have no effect, and the presence of such code which deletes a pointer which sometimes contains NULL is not necessarily bad. –  Brian R. Bondy Nov 16 '10 at 13:02
IMO redundant checks certainly are bad, for performance, readability and maintainability. –  paulm Feb 25 at 0:56
add comment

From the C++0x draft Standard.

$5.3.5/2 - "[...]In either alternative, the value of the operand of delete may be a null pointer value.[...'"

Of course, no one would ever do 'delete' of a pointer with NULL value, but it is safe to do. Ideally one should not have code that does deletion of a NULL pointer. But it is sometimes useful when deletion of pointers (e.g. in a container) happens in a loop. Since delete of a NULL pointer value is safe, one can really write the deletion logic without explicit checks for NULL operand to delete.

As an aside, C Standard $ also says that 'free' on a NULL pointer does no action.


share|improve this answer
add comment

I have experienced that it is not safe (VS2010) to delete[] NULL (i.e. array syntax). I'm not sure whether this is according to the C++ standard.

It is safe to delete NULL (scalar syntax).

share|improve this answer
This is illegal, and I don’t believe it. –  Konrad Rudolph Aug 30 '13 at 16:56
You should be able to delete any null pointer. So if it's breaking for you, then you probably have a bug in the code that shows it. –  Mysticial Aug 30 '13 at 17:00
add comment

Your Answer


