Why doesn't delete set the pointer to NULL?

aJ. picture aJ. · Apr 1, 2009 · Viewed 51.8k times · Source

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?

Answer

Dan Olson picture Dan Olson · Apr 1, 2009

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.