What's the difference between delete-ing a pointer and setting it to nullptr? [duplicate]

喜你入骨 提交于 2019-12-01 19:32:14

It is not the same, because while you may be setting the pointer to null, the contents that the pointer pointed to would still be taking up space.

Doing

delete pointer;
pointer = NULL;

Is fine, but

pointer = NULL;
delete pointer;

Is not, since you already set the pointer to NULL, the delete command will have nothing to delete (or so it thinks). You now have a memory leak because whatever the pointer pointed to beforehand (let's say a linked list) is now floating somewhere in your memory and is untrackable by the program.

pointer = nullptr; is like removing the ink from a business card. You no longer know where the house is located, at least not from looking at that particular business card. But the house is still there.

delete pointer; is like tearing down the house. The business card still tells you where that house used to be, but if you were to drive there (dereference the pointer), you would see the house was gone. Or worse, maybe they put up a nuclear waste storage facility at that address in the meantime.

Is saying delete pointer and pointer = nullptr the same? Probably not, but does the latter free up memory?

A delete expression calls the destructor and deallocates the memory (i.e., returns it to the free store). Setting the pointer to a null pointer does neither of these, and may leak memory or resources if there are no other pointers to the object.

What about delete pointer; pointer = nullptr / pointer = nullptr; delete pointer?

If you delete a pointer that is already null, there is no effect. So the former destroys the object and deallocates the memory and then sets the pointer to null, whereas the latter still leaks because the delete has no effect.

Some people recommend setting a pointer to null after it is deleted, so that if it's deleted a second time due to a bug, it doesn't crash the program. Personally I don't recommend this; I think deleting a pointer twice is a bug even if it has no effect the second time, and that it's good if the program crashes so you can find and fix that bug.

Why not use that to make a safe way to delete pointers prematurely if required, where they would normally be deleted some other time and cause an error with a normal delete?

Not sure what you mean...

deleting the pointer frees the memory that the pointer points to. Just setting the pointer to nullptr will cause a memeory leak as there is no way to now delete the memory the pointer was pointing to.

You can set a pointer to nullptr after you delete it though as it tells you the pointer points to nothing now and if you call delete on it again by accident it is a non op and your program will continue to run.

delete is called that not only to free allocated memory for the object but also that to call the destructor of the object.

If the destructor will not be called then the object will still keep resources.

Of course fundamental types do not have destructors and calling delete will only free memory occupied by an object of a fundamental type.

But in general objects of user-defined types require to call their destructors when they are deleted.

And the pointer shall points to an object. So there is no sense in this sequence of statements

pointer = nullptr; delete pointer;

because nullptr is not a valid address of an object. It is a NULL pointer literal.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!