问题
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?
回答1:
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.
回答2:
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.
回答3:
Is saying
delete pointer
andpointer = 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...
回答4:
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.
回答5:
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.
来源:https://stackoverflow.com/questions/31618975/whats-the-difference-between-delete-ing-a-pointer-and-setting-it-to-nullptr