C++ – Why doesn’t delete set the pointer to NULL

c++delete-operatormemory-management

I always wondered why automatic setting of the pointer to NULL after delete is not part of the standard. If this gets taken care of then many of the crashes due to an invalid pointer would not occur. But having said that I can think of couple of reasons why the standard would have restricted this:

  1. Performance:

    An additional instruction could slow down the delete performance.

  2. Could it be because of const pointers.

    Then again standard could have done something for this special case I guess.

Does anyone know exact reasons for not allowing this?

Best Solution

Stroustrup himself answers. An excerpt:

C++ explicitly allows an implementation of delete to zero out an lvalue operand, and I had hoped that implementations would do that, but that idea doesn't seem to have become popular with implementers.

But the main issue he raises is that delete's argument need not be an lvalue.