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

5

9

Is saying delete pointer and pointer = nullptr the same? Probably not, but does the latter free up memory? What about delete pointer; pointer = nullptr / pointer = nullptr; delete pointer? 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?

Overcurious answered 24/7, 2015 at 20:10 Comment(0)
V
10

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.

Valvular answered 24/7, 2015 at 20:12 Comment(5)
But with delete pointer; pointer = nullptr would that make it not freak out when trying to delete a pointer that's already deleted? Are there any problems with that?Overcurious
You aren't getting it. Setting a pointer to null is not deleting it. It is just setting it to point to null.Valvular
setting pointer = NULL before deleting and then deleting may result in unexpected behaviour depending on the implementation.Kraska
setting a pointer to null doesn't "delete" it. the memory that the pointer is pointing at is still allocated and marked as in-use by the system. you've just told your code to forget where it is - so now you leaked some memory. you app doesn't know the ram's there, because you forgot about it, so you can't "free" it. and the memory system won't free it for you, since it doesn't know WHAT you're using this memory for or that you're done with it.Peshawar
@Omega delete pointer; pointer = nullptr. just deletes the pointer. setting it to null after deleting is fine. Setting null to a pointer is not same as deleting.Kraska
K
19

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.

Kumasi answered 24/7, 2015 at 20:17 Comment(2)
To expand upon this analogy, you can do pointer = nulptr; as often as you wish, as there is still a business card, but no ink to be removed, so it has no ill effect (besides losing track of where the house is located). Trying to call delete pointer; after the pointer has already been deleted is bad as you're trying to now delete a house that no longer exists. Trying to call delete pointer after pointer = nullptr is also bad because you're now trying to delete a house without knowing where to look.Planetoid
@FaithForHumans delete nullptr is defined to be a no op.Kumasi
V
10

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.

Valvular answered 24/7, 2015 at 20:12 Comment(5)
But with delete pointer; pointer = nullptr would that make it not freak out when trying to delete a pointer that's already deleted? Are there any problems with that?Overcurious
You aren't getting it. Setting a pointer to null is not deleting it. It is just setting it to point to null.Valvular
setting pointer = NULL before deleting and then deleting may result in unexpected behaviour depending on the implementation.Kraska
setting a pointer to null doesn't "delete" it. the memory that the pointer is pointing at is still allocated and marked as in-use by the system. you've just told your code to forget where it is - so now you leaked some memory. you app doesn't know the ram's there, because you forgot about it, so you can't "free" it. and the memory system won't free it for you, since it doesn't know WHAT you're using this memory for or that you're done with it.Peshawar
@Omega delete pointer; pointer = nullptr. just deletes the pointer. setting it to null after deleting is fine. Setting null to a pointer is not same as deleting.Kraska
A
4

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...

Aslam answered 24/7, 2015 at 20:15 Comment(0)
B
2

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.

Badalona answered 24/7, 2015 at 20:14 Comment(0)
C
0

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.

Commensurate answered 24/7, 2015 at 20:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.