链表内存管理
如何释放包含动态分配对象的链表?
我尝试使用 list
列表,但随后我无法使用 insert()
函数将对象插入到列表中。 有谁知道是什么原因?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如何释放包含动态分配对象的链表?
我尝试使用 list
列表,但随后我无法使用 insert()
函数将对象插入到列表中。 有谁知道是什么原因?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(8)
std::list >
当列表被删除时,将自动在列表中的每个(智能)指针上调用YourType::~YourType
。 如果您删除一个列表元素,它将为该元素调用YourType::~YourType
。 您仍然可以调用list.insert(new YourType
)链接:www.boost.org
std::list<boost::shared_ptr<YourType> >
will automatically callYourType::~YourType
on each (smart) pointer in the list, when the list is deleted. And if you erase one list element, it will callYourType::~YourType
for that one element. You can still calllist.insert(new YourType
)Link: www.boost.org
遍历列表,删除每个链接所包含的对象,或者在某些情况下更好,编写一个删除所包含对象的链接 dtor。
没有看到你的代码就不行。
Walk the list, deleting the contained object for each link, or better in some cases, write a link dtor that deletes the contained object.
Not without seeing your code.
从头到尾遍历列表,逐一删除。
Traverse the list from the beginning to end, and delete one by one.
STL使用的解决方案是让容器拥有它所包含的对象。 在这种情况下,每个节点将负责在调用其析构函数时释放其对象。 您仍然负责释放传入对象的副本。
编辑
如果您尝试使用 stl List 并遇到错误,可能是您尝试放入其中的对象未实现复制构造函数。 如果您尝试将指针以外的内容放入列表中,则可能会发生这种情况。 如果您对指针感到满意,您可以考虑将它们存储在容器中而不是对象本身。 这将要求您在完成操作后在指针上调用“删除”自己。 这通常是通过在删除节点时对节点中包含的指针调用“delete”来完成的,或者在完成节点后遍历列表并对每个节点的内容调用“delete”来完成。
The solution used by the STL is to have the container own the objects it contains. In this scenario each node would be responsible for deallocating it's object when it's destructor is called. You're still responsible for deallocating the copy of the object you pass in.
Edit
If you're attempting to use the stl List and encountering an error, it might be the case that the object you're trying to place inside does not implement a copy constructor. This can happen if you try to stick things other than pointers into the list. If you're comfortable with pointers you might consider storing them in your containers instead of the objects themselves. This will require that you call 'delete' yourself on the pointer when you're done with it. This is typically done by calling 'delete' on the pointer contained in a node when you remove it or by walking the list when you're done with it and calling 'delete' on the contents of each node.
在清除列表之前,您必须迭代列表并
删除
动态分配的对象。关于使用
insert()
的问题:我不太确定您面临的问题是什么。 根据解释,我假设您正在尝试将Class
的对象插入列表中,因为列表包含指向Class
的指针。 您能详细说明一下插入的问题吗?删除动态分配对象的伪代码:
版本1:简单
版本2:使用for_each的更好版本
You will have to iterate the list and
delete
the dynamically allocated objects, before clearing the list.About problem using
insert()
: I am not very sure what is the problem you are facing. From the explanation, I assume, you are trying to insert objects ofClass
into list where as the list contains the pointer toClass
. Could you please elaborate the problem with insert?Pseudo code for deleting the dynamically allocated objects:
version 1: Simple
version 2: A better version using for_each
您可以使用Boost
http://www.ptr_list。 boost.org/doc/libs/1_38_0/libs/ptr_container/doc/ptr_list.html
You could use ptr_list from Boost
http://www.boost.org/doc/libs/1_38_0/libs/ptr_container/doc/ptr_list.html
编辑:这是错误的,抱歉
如果您使用 std::list 那么有一个明确的函数可以删除您放入的所有对象。
std::列表测试;
测试.clear();
哦,.clear() 还会调用析构函数。
要使用此类列表的插入功能,您需要通过迭代器指定位置。
测试.插入(测试.结束(), 5);
但是插入函数不止1个,在这里你可以找到更多详细信息
编辑
有人可以在投票时发表评论吗?
EDIT: THIS IS WRONG, SORRY
If you're using std::list then there's a clear function to delete all objects you put in.
std::list test;
test.clear();
Oh, and .clear() also invokes the destructors.
To use the insert function of such lists you need to specify the position via an iterator.
test.insert(test.end(), 5);
But there's more than 1 insert function, here you can find more details
EDIT
Could somebody leave a comment when downvoting?
在具有自动内存管理(垃圾收集)的语言/环境中启动您的项目。 那么诸如此类的事情将是你最不关心的。 这会节省你的时间,让你把精力投入到其他事情上。
Start your project in language/environment, which having an automatic memory management (Garbage collection). Then things like that will be the least which you care about. It will save your time to put your efforts into something else.