std::list::remove 方法是否调用每个被删除元素的析构函数?
我有代码:
std::list<Node *> lst;
//....
Node * node = /* get from somewhere pointer on my node */;
lst.remove(node);
std::list::remove
方法是否调用每个已删除元素的析构函数(和可用内存)?如果是这样,我该如何避免呢?
I have the code:
std::list<Node *> lst;
//....
Node * node = /* get from somewhere pointer on my node */;
lst.remove(node);
Does the std::list::remove
method call the destructor (and free memory) of each removed element? If so, how I can avoid it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
是的,从容器中删除
Foo*
会破坏Foo*
,但不会释放Foo
。销毁原始指针总是是一个空操作。不可能有其他方式!让我给你几个理由。存储类
只有当指针对象实际上是动态分配的时候,删除指针才有意义,但是运行时怎么可能知道指针变量被销毁时是否是这种情况呢?指针还可以指向静态变量和自动变量,删除其中之一会产生未定义的行为。
悬空指针
无法确定指针对象过去是否已经被释放。两次删除同一个指针会产生未定义的行为。 (第一次删除后,它变成悬空指针。)
未初始化的指针
也无法检测指针变量是否已初始化。猜猜当你尝试删除这样的指针时会发生什么?再次,答案是未定义的行为。
动态数组
类型系统不区分指向单个对象的指针 (
Foo*
) 和指向对象数组的第一个元素的指针(也是Foo*
) 。当指针变量被销毁时,运行时无法确定是通过delete
还是通过delete[]
释放指针。通过错误的形式释放会调用未定义的行为。总结
由于运行时无法对指针对象执行任何明智的操作,因此销毁指针变量始终是无操作的。什么都不做肯定比由于不知情的猜测而导致未定义的行为要好:-)
建议
考虑使用智能指针作为容器的值类型,而不是原始指针,因为它们负责在不再需要指针时释放指针。根据您的需要,使用
std::shared_ptr
或std::unique_ptr
。如果您的编译器尚不支持 C++0x,请使用boost::shared_ptr
。从不,我重复一遍,从不使用
std::auto_ptr作为容器的值类型。
Yes, removing a
Foo*
from a container destroys theFoo*
, but it will not release theFoo
. Destroying a raw pointer is always a no-op. It cannot be any other way! Let me give you several reasons why.Storage class
Deleting a pointer only makes sense if the pointee was actually allocated dynamically, but how could the runtime possibly know whether that is the case when the pointer variable is destroyed? Pointers can also point to static and automatic variables, and deleting one of those yields undefined behavior.
Dangling pointers
There is no way to figure out whether the pointee has already been released in the past. Deleting the same pointer twice yields undefined behavior. (It becomes a dangling pointer after the first delete.)
Uninitialized pointers
It is also impossible to detect whether a pointer variable has been initialized at all. Guess what happens when you try to delete such a pointer? Once again, the answer is undefined behavior.
Dynamic arrays
The type system does not distinguish between a pointer to a single object (
Foo*
) and a pointer to the first element of an array of objects (alsoFoo*
). When a pointer variable is destroyed, the runtime cannot possibly figure out whether to release the pointee viadelete
or viadelete[]
. Releasing via the wrong form invokes undefined behavior.Summary
Since the runtime cannot do anything sensible with the pointee, destroying a pointer variable is always a no-op. Doing nothing is definitely better than causing undefined behavior due to an uninformed guess :-)
Advice
Instead of raw pointers, consider using smart pointers as the value type of your container, because they take responsibility for releasing the pointee when it is no longer needed. Depending on your need, use
std::shared_ptr<Foo>
orstd::unique_ptr<Foo>
. If your compiler does not support C++0x yet, useboost::shared_ptr<Foo>
.Never, I repeat, NEVER EVER use
std::auto_ptr<Foo>
as the value type of a container.它调用
list
中每个项目的析构函数——但这不是Node
对象。它是一个节点*
。所以它不会删除
Node
指针。这有道理吗?
It calls the destructor of each of the items in the
list
-- but that's not aNode
object. Its aNode*
.So it doesn't delete the
Node
pointers.Does that make sense?
它确实调用列表中数据的析构函数。这意味着, std::list::remove 将调用
T
的析构函数(当T
类似于 <代码>std::向量)。在您的情况下,它将调用 Node* 的析构函数,这是一个无操作。它不会调用
node
的析构函数。It does call the destructor of the data in the list. That means,
std::list<T>::remove
will call the destructor ofT
(which is necessary whenT
is something likestd::vector
).In your case, it would call the destructor of
Node*
, which is a no-op. It doesn't call the destructor ofnode
.是的,尽管在这种情况下,Node* 没有析构函数。不过,根据其内部结构,各种 Node* 值会被范围规则删除或销毁。如果 Node* 是一些非基本类型,则会调用析构函数。
析构函数是在 Node 上调用的吗?不,但“Node”不是列表中的元素类型。
至于你的另一个问题,你不能。标准列表容器(实际上所有标准容器)采用其内容的所有权并将其清理。如果您不希望这种情况发生,那么标准容器不是一个好的选择。
Yes, though in this case, Node* has no destructor. Depending on its internals though, the various Node* values are either deleted or destroyed by scoping rules. If Node* where some non-fundamental type, a destructor would be called.
Is the destructor called on the Node? No, but 'Node' is not the element type in the list.
As to your other question, you can't. The standard list container (in fact ALL standard containers) adopt ownership of their content and will clean it up. If you don't want this to happen, the standard containers are not a good choice.
由于您将指针放入
std::list
中,因此不会在指向的Node
对象上调用析构函数。如果您想将堆分配的对象存储在 STL 容器中并在删除时销毁它们,请将它们包装在像
boost::shared_ptr
这样的智能指针中Since you are putting pointers into a
std::list
, destructors are not called on the pointed-toNode
objects.If you want to store heap allocated objects in STL containers and have them be destructed upon removal, wrap them in a smart pointer like
boost::shared_ptr
最好的理解方法是测试每种形式并观察结果。要熟练地将容器对象与您自己的自定义对象一起使用,您需要对其行为有很好的理解。
简而言之,对于 Node* 类型,既不调用解构函数,也不调用删除/释放;但是,对于 Node 类型,将调用解构函数,同时考虑删除/释放是列表的实现细节。意思是,这取决于列表实现是否使用 new/malloc。
对于
unique_ptr
,解构函数会被调用,并且会调用delete/free,因为您必须给它由new
分配的东西。请特别注意内存地址。您可以通过范围判断哪些指向堆栈,哪些指向堆。
The best way to understand is to test each form and observe the results. To skillfully use the container objects with your own custom objects you need to have a good understanding of the behavior.
In short, for the type
Node*
neither the deconstructor is called nor delete/free is invoked; however, for the typeNode
the deconstructor would be invoked while consideration of delete/free is an implementation detail of list. Meaning, it depends on if the list implementation used new/malloc.In the case of a
unique_ptr<Node>
, the deconstructor is invoked and the calling of delete/free will happen since you had to give it something allocated bynew
.Pay careful attention to the memory addresses. You can tell which are pointing into the stack and which are pointing into the heap by the ranges.