健全性检查 - 当容器本身死亡时,新对象的 stl::Container 是否会被删除?

发布于 2024-09-01 21:04:57 字数 87 浏览 1 评论 0原文

标题几乎涵盖了它。如果我将 3 个对象添加到列表中,并且列表超出范围并消失,那么它会在超出范围之前对每个条目调用删除吗?肯定是的,但是累了,需要进行健全性检查。

Title pretty much covers it. If I've added say 3 objects to a list and the list goes out of scope and dies, will it call delete on each entry before going out of scope? Pretty sure yes, but getting tired and need a sanity check.

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

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

发布评论

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

评论(5

谁的新欢旧爱 2024-09-08 21:04:57

调查显示:我累了,不,他们不会删除自己,除非它们位于列表本身的某种容器安全智能指针内。午睡时间...

Survey says: I'm tired and no they won't delete themeselves unless they are within some sort of container safe smart pointer in the list itself. Nap time...

云醉月微眠 2024-09-08 21:04:57

不,任何 STL 容器都不会删除您的指针。这是因为 STL 容器永远不会拥有指针的所有权,因此 STL 容器永远不会承担双重删除的责任。

No, no STL container will ever delete your pointers. This because STL container never take ownership of your pointers, and so STL containers never take the blame for double deletes.

执着的年纪 2024-09-08 21:04:57

如果您有一个 std::list; my_list; 然后 my_list 超出范围,列表中的每个 T 对象都将被删除。

现在的问题是,您在容器中存储哪些对象?

如果您的容器中有真实的对象,例如 std::list,则这些对象将被删除,并且将为每个对象调用 MyClass::~MyClass他们。

相反,如果您只有指向对象的指针,例如 std::list,则指针将被删除,但指向的对象将不会被删除!

如果您需要存储指向对象的指针并希望在容器死亡时销毁指向的对象,则必须使用一些智能指针(例如boost::smart_ptr*),当指针本身被删除时,它们会删除它们的对象。或者,您需要在销毁指针容器之前手动遍历列表并销毁对象,但这更容易出错,并且可能需要额外的工作来确保在发生异常时执行此操作。

*std::auto_ptr 有一个不寻常的operator=(),它与容器的要求不兼容。 (感谢 j-random-hacker ——请在下面给他 +1...)

If you've got a std::list<T> my_list; and then my_list goes out of scope, each T object within the list will be deleted.

The question now is, what objects are you storing in your container?

If you have real objects in your container, e.g. std::list<MyClass>, the objects will be deleted and MyClass::~MyClass will be called for each one of them.

If instead you only have pointers to objects, e.g. std::list<MyClass*>, the pointers will be deleted, but the pointed-to objects will not be deleted!

If you need to store pointers to objects and want the pointed-to objects to be destroyed when the container dies, you'll have to use some some of smart pointers (e.g. boost::smart_ptr*), which delete their object as the pointers themselves get deleted. Or you'll need to manually go through the list and destroy objects before you destroy the container of pointers, but this is more error-prone and may need extra work to make sure you do that in case of an exception.

*std::auto_ptr has an unusual operator=() which is incompatible with the requirements of containers. (Thanks j-random-hacker -- please give him +1 below...)

不念旧人 2024-09-08 21:04:57

No, the objects will not be deleted.

Boost has a solution, the Boost Pointer Container Library. Not only does it delete the pointers for you, but it enhances the syntax of common operations to make them more convenient.

挽你眉间 2024-09-08 21:04:57

不会。不会对每个项目调用“删除”。如果您希望发生这种情况,那么列表应该保存智能指针而不是普通指针。请参阅C++ STL 指针向量。如果您确实不想在应用程序中出现内存泄漏,则需要在列表超出范围之前对每个指针显式调用删除。

No. 'delete' will not be called on each item. If you want that to happen, then the list should hold smart pointers instead of plain pointers. Refer C++ STL vector of pointers. If you really hate to have a memory leak in your application, you need to call delete explicitly on each pointer before the list goes out of scope.

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