链表内存管理

发布于 2024-07-18 05:27:12 字数 124 浏览 5 评论 0 原文

如何释放包含动态分配对象的链表? 我尝试使用 list 列表,但随后我无法使用 insert() 函数将对象插入到列表中。 有谁知道是什么原因?

How could I free up a linked list that contains dynamically allocated objects?
I try to use list<class*> lists, but then I could not use the insert() function to insert object to the list. Does anyone know what is the cause?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

嘿哥们儿 2024-07-25 05:27:12

std::list > 当列表被删除时,将自动在列表中的每个(智能)指针上调用 YourType::~YourType。 如果您删除一个列表元素,它将为该元素调用 YourType::~YourType。 您仍然可以调用 list.insert(new YourType)

链接:www.boost.org

std::list<boost::shared_ptr<YourType> > will automatically call YourType::~YourType on each (smart) pointer in the list, when the list is deleted. And if you erase one list element, it will call YourType::~YourType for that one element. You can still call list.insert(new YourType)

Link: www.boost.org

半暖夏伤 2024-07-25 05:27:12

如何释放包含动态分配对象的链表?

遍历列表,删除每个链接所包含的对象,或者在某些情况下更好,编写一个删除所包含对象的链接 dtor。

我尝试使用列表列表,但后来我无法使用 insert() 函数将对象插入列表。 有谁知道是什么原因吗?

没有看到你的代码就不行。

how could I free up linked list that contain dynamically allocated object?

Walk the list, deleting the contained object for each link, or better in some cases, write a link dtor that deletes the contained object.

I try to use list lists, but then I could not use insert()function to insert object to the list. Does anyone know what is the cause?

Not without seeing your code.

相思故 2024-07-25 05:27:12

从头到尾遍历列表,逐一删除。

Traverse the list from the beginning to end, and delete one by one.

南烟 2024-07-25 05:27:12

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.

月亮坠入山谷 2024-07-25 05:27:12

在清除列表之前,您必须迭代列表并删除动态分配的对象。

当使用新的容器时
指点一下,请记得删除
容器之前的指针
被摧毁。

关于使用 insert() 的问题:我不太确定您面临的问题是什么。 根据解释,我假设您正在尝试将 Class 的对象插入列表中,因为列表包含指向 Class 的指针。 您能详细说明一下插入的问题吗?

删除动态分配对象的伪代码:

版本1:简单

list<Class*>::const_iterator iter = m_ClassList.begin();
list<Class*>::const_iterator endIter = m_ClassList.end();
for(;iter!= endIter ; ++iter)
{
   delete *iter;
}
ClassList.clear();

版本2:使用for_each的更好版本

struct DeleteClassObject 
{
  //Functor
  template<typename T>
  void operator()(const T* ptr) const
  {
      delete ptr;
   }
}
//loops through list and deletes the dynamically allocated objects using
//functor DeleteClassObject
for_each( m_ClassList.begin(), m_ClassList.end(), DeleteClassObject ());
DeleteClassObject.clear()

You will have to iterate the list and delete the dynamically allocated objects, before clearing the list.

When using containers of newed
pointers, please remember to delete
the pointers before the container is
destroyed.

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 of Class into list where as the list contains the pointer to Class. Could you please elaborate the problem with insert?

Pseudo code for deleting the dynamically allocated objects:

version 1: Simple

list<Class*>::const_iterator iter = m_ClassList.begin();
list<Class*>::const_iterator endIter = m_ClassList.end();
for(;iter!= endIter ; ++iter)
{
   delete *iter;
}
ClassList.clear();

version 2: A better version using for_each

struct DeleteClassObject 
{
  //Functor
  template<typename T>
  void operator()(const T* ptr) const
  {
      delete ptr;
   }
}
//loops through list and deletes the dynamically allocated objects using
//functor DeleteClassObject
for_each( m_ClassList.begin(), m_ClassList.end(), DeleteClassObject ());
DeleteClassObject.clear()
巴黎夜雨 2024-07-25 05:27:12

编辑:这是错误的,抱歉

如果您使用 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?

梦在深巷 2024-07-25 05:27:12

在具有自动内存管理(垃圾收集)的语言/环境中启动您的项目。 那么诸如此类的事情将是你最不关心的。 这会节省你的时间,让你把精力投入到其他事情上。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文