Is It Safe to Delete a Null Pointer

Is it safe to delete a NULL pointer?

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?)

Is it safe to delete a nullptr twice in C++?

Is it safe to delete a nullptr twice in C++?

Yes (in all standard versions of C++). It is guarnteed to work (by work, I mean it doesn't do anyting).

Deletion has no effects if the argument is a null pointer. The compiler that the presenter describes did not conform to the C++ standard. The described compiler also was from the 80's, so it was made before C++ was standardised. The presenter is wrong in saying that you can't delete the null pointer twice if they are referring to standard C++ which does seem to be implied.

It is true that deletion may indirectly cause the program to behave as if the value of the argument pointer was changed (and by as-if rule that means that they effectively can change the value), but only in case where the pointer was non-null and is thus invalidated by the deletion. In fact, this allowance applies to all pointer objects that had the same value as all of them are thereby invalidated. This is because all ways that could observe the value of an invalid pointer are either undefined or implementation defined behaviour.

Calling delete or delete[] on NULL pointer

Yes, the standard, since C++98, guarantees that delete or delete[] on a nullpointer has no effect.

C++98 §5.3.5/2

In either alternative, if the value of the operand of delete is the null pointer the operation has no effect.

This was so also before the first standard, when the language was defined by the Annotated Reference Manual.


version compiled for Linux with g++ (unfortunately, I do not remember the g++ version) was throwing NULL pointer reference

That's impossible to discuss without a concrete and preferably complete example that reproduces the behavior. It had nothing to do with deleting a nullpointer.

Deleting NULL pointer memory

Yes, you got memory leaks here. Memory leaks are common problems, but they won't cause your program to be killed immediately, sometimes when the system memory is running out, the OS will kill other programs that are not leaking, they are victims.

Delete a NULL pointer is always safe, and it's duplicated. What you have learned is wrong. And you may also view the doc from cpp reference.

For modern c++, it's always recommended to use smart pointers, we don't delete pointers by hand anymore, most leaks are avoided.

Is it still safe to delete nullptr in c++0x?

5.3.5/7 says:

If the value of the operand of the delete-expression is not a null pointer value, the delete-expression will call a deallocation function ( Otherwise, it is unspecified whether the deallocation function will be called.

And says:

The value of the first argument supplied to a deallocation function may be a null pointer value; if so, and if the deallocation function is one supplied in the standard library, the call has no effect.

So the behavior is well defined, as long as the standard deallocation function is used, or a user-provided deallocation function handles null pointers correctly.

Is it good practice to NULL a pointer after deleting it?

Setting a pointer to 0 (which is "null" in standard C++, the NULL define from C is somewhat different) avoids crashes on double deletes.

Consider the following:

Foo* foo = 0; // Sets the pointer to 0 (C++ NULL)
delete foo; // Won't do anything


Foo* foo = new Foo();
delete foo; // Deletes the object
delete foo; // Undefined behavior

In other words, if you don't set deleted pointers to 0, you will get into trouble if you're doing double deletes. An argument against setting pointers to 0 after delete would be that doing so just masks double delete bugs and leaves them unhandled.

It's best to not have double delete bugs, obviously, but depending on ownership semantics and object lifecycles, this can be hard to achieve in practice. I prefer a masked double delete bug over UB.

Finally, a sidenote regarding managing object allocation, I suggest you take a look at std::unique_ptr for strict/singular ownership, std::shared_ptr for shared ownership, or another smart pointer implementation, depending on your needs.

Is there any reason to check for a NULL pointer before deleting?

It's perfectly "safe" to delete a null pointer; it effectively amounts to a no-op.

The reason you might want to check for null before you delete is that trying to delete a null pointer could indicate a bug in your program.


NOTE: if you overload the delete operator, it may no longer be "safe" to delete NULL

What C++17 standard say about calling delete on nullptr?

Yes it is valid, and it results in a noop. reference

If expression evaluates to a null pointer value, no destructors are called, and the deallocation function is not called.

Why is it recommended to set a pointer to null after deleting it?

Just so that you know the pointer does not point to anything anymore, and will fail if conditions and other boolean checks:

   delete ptr;
ptr = NULL;
*ptr = 2;

This code will run perfectly fine, although it would cause memory corruption if the pointer was not set to NULL.

Deleting a null pointer

delete will check if the pointer is NULL for you, so you're right that the check isn't needed.

You might also see that some people set the pointer to NULL after it's deleted so that you don't do anything stupid like try and use memory that is no longer yours or stop you from deleting the pointer twice, which will cause an error.

Related Topics

Leave a reply