Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't delete set the pointer to NULL?

People also ask

Does delete set a pointer to null?

In this case delete operation on one pointer would make only that pointer NULL (if delete was making pointer NULL) and the other pointer would be non-NULL and pointing to memory location which is free. The solution for this should have been that user should delete all pointers pointing to same location.

Can you set pointers to null?

We can directly assign the pointer variable to 0 to make it null pointer.

Does delete destroy the pointer?

Yes, it destroys the object by calling its destructor. It also deallocates the memory that new allocated to store the object. Or does it only destory the pointer? It does nothing to the pointer.

What happens to pointer after delete?

The address of the pointer does not change after you perform delete on it. The space allocated to the pointer variable itself remains in place until your program releases it (which it might never do, e.g. when the pointer is in the static storage area).


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.


First, setting to null would require a memory stored variable. It's true, that you usually have a pointer in a variable but sometimes you might want to delete an object at a just calculated address. That would be impossible with "nullifying" delete.

Then comes performance. You might have written code in such a way that the pointer will go out of scope immediately after delete is done. Filling it with null is just a waste of time. And C++ is a language with "don't need it? then you don't have to pay for it" ideology.

If you need safety there's a wide range of smart pointers at you service or you can write your own - better and smarter.


You can have multiple pointers pointing to that memory. It would create a false sense of security if the pointer you specified for the delete got set to null, but all the other pointers did not. A pointer is nothing more than an address, a number. It might as well be an int with a dereference operation. My point is you would have to also scan every single pointer to find those that are referencing the same memory you just deleted, and null them out as well. It would be computationally intense to scan all the pointers for that address and null them out, because the language is not designed for that. (Although some other languages structure their references to accomplish a similar goal in a different way.)


A pointer can be saved in more than one variable, setting one of these to NULL would still leave invalid pointers in the other variables. So you don't really gain much, you are more likely creating a false sense of security.

Besides of that, you can create your own function that does what you want:

template<typename T>
void deleten(T *&ptr) {
  delete ptr;
  ptr = NULL;
}

Because there isn't really any need to, and because it would require delete taking pointer-to-pointer rather than just pointer.


delete is used mostly in destructors, in which case setting a member to NULL is pointless. A few lines later, at the closing }, the member no longer exists. In assignment operators, a delete is typically followed by an assignment anyway.

Also, it would render the following code illegal:

T* const foo = new T;
delete foo;