This question already has an answer here:
这个问题在这里已有答案:
- delete vs NULL vs free in c++ 6 answers
在c ++ 6答案中删除vs NULL vs free
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?
是说删除指针和指针= nullptr是一样的吗?可能不是,但后者会释放内存吗?删除指针怎么样; pointer = nullptr / pointer = nullptr;删除指针?为什么不使用它来提供一种安全的方法来过早地删除指针,如果需要,通常会在其他时间删除它们并导致正常删除错误?
5 个解决方案
#1
7
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.
它不一样,因为虽然您可能将指针设置为null,但指针指向的内容仍将占用空间。
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.
不是,因为你已经将指针设置为NULL,所以删除命令将没有任何内容可以删除(或者它认为)。你现在有一个内存泄漏,因为预先指向的指针(假设一个链表)现在浮动在你的内存中,并且程序无法检查。
#2
9
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.
pointer = nullptr;就像从名片上取下墨水一样。你不再知道房子的位置,至少不是看那张特定的名片。但房子还在那里。
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
3
Is saying
delete pointer
andpointer = nullptr
the same? Probably not, but does the latter free up memory?是说删除指针和指针= nullptr是一样的吗?可能不是,但后者会释放内存吗?
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?
删除指针怎么样; pointer = nullptr / pointer = nullptr;删除指针?
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.
如果删除已经为null的指针,则无效。所以前者破坏对象并释放内存,然后将指针设置为null,而后者仍然泄漏,因为删除无效。
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.
有些人建议在删除指针后将其设置为null,这样如果由于错误而第二次删除它,它就不会使程序崩溃。我个人不推荐这个;我认为删除一个指针两次是一个错误,即使它第二次没有效果,并且如果程序崩溃,那么你可以找到并修复该错误是好的。
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
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.
删除指针会释放指针指向的内存。只是将指针设置为nullptr将导致记忆泄漏,因为现在无法删除指针所指向的内存。
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.
你删除它后可以设置一个指向nullptr的指针,因为它告诉你现在指针指向什么,如果你再次调用delete就意外它是非操作,你的程序将继续运行。
#5
0
delete is called that not only to free allocated memory for the object but also that to call the destructor of the object.
调用delete不仅可以释放对象的已分配内存,还可以调用对象的析构函数。
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.
当然基本类型没有析构函数,调用delete只会释放基本类型对象占用的内存。
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.
因为nullptr不是对象的有效地址。它是一个NULL指针文字。
#1
7
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.
它不一样,因为虽然您可能将指针设置为null,但指针指向的内容仍将占用空间。
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.
不是,因为你已经将指针设置为NULL,所以删除命令将没有任何内容可以删除(或者它认为)。你现在有一个内存泄漏,因为预先指向的指针(假设一个链表)现在浮动在你的内存中,并且程序无法检查。
#2
9
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.
pointer = nullptr;就像从名片上取下墨水一样。你不再知道房子的位置,至少不是看那张特定的名片。但房子还在那里。
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
3
Is saying
delete pointer
andpointer = nullptr
the same? Probably not, but does the latter free up memory?是说删除指针和指针= nullptr是一样的吗?可能不是,但后者会释放内存吗?
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?
删除指针怎么样; pointer = nullptr / pointer = nullptr;删除指针?
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.
如果删除已经为null的指针,则无效。所以前者破坏对象并释放内存,然后将指针设置为null,而后者仍然泄漏,因为删除无效。
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.
有些人建议在删除指针后将其设置为null,这样如果由于错误而第二次删除它,它就不会使程序崩溃。我个人不推荐这个;我认为删除一个指针两次是一个错误,即使它第二次没有效果,并且如果程序崩溃,那么你可以找到并修复该错误是好的。
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
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.
删除指针会释放指针指向的内存。只是将指针设置为nullptr将导致记忆泄漏,因为现在无法删除指针所指向的内存。
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.
你删除它后可以设置一个指向nullptr的指针,因为它告诉你现在指针指向什么,如果你再次调用delete就意外它是非操作,你的程序将继续运行。
#5
0
delete is called that not only to free allocated memory for the object but also that to call the destructor of the object.
调用delete不仅可以释放对象的已分配内存,还可以调用对象的析构函数。
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.
当然基本类型没有析构函数,调用delete只会释放基本类型对象占用的内存。
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.
因为nullptr不是对象的有效地址。它是一个NULL指针文字。