Sobes.tech
Junior — Middle

Is it safe to call the delete operator on a null pointer?

sobes.tech AI

Answer from AI

Yes, in C++ the delete operator can be safely called on a null (nullptr) pointer. The language standard states that calling delete on a null pointer has no effect and does nothing.

Example:

int* p = nullptr;
delete p; // Safe, nothing happens

This allows you to omit null pointer checks before deletion, simplifying code and preventing potential errors.