Should I Check if a Pointer Is NULL Before Deleting It?

Most of the time I see some C++ programmers who check if a pointer is NULL before deleting it.

if (ptr != NULL) {
    delete ptr;
    ptr = NULL;
}

Well, according to C++03 [ISO/IEC IS 14882:2003] §5.3.5/2 which explicitly states:

…if the value of the operand of delete is the null pointer the operation has no effect.

Therefore, deleting a NULL pointer has no effect (if the deallocation function is one supplied in the standard library), so it is not necessary to check for a NULL pointer before calling delete.

delete ptr;
ptr = NULL;
  • Note: Keep in mind that deleting a void* pointer results in undefined behavior whether it’s NULL or not.