使用 new 关键字和 STL 指针向量进行内存管理

发布于 2024-08-15 11:55:08 字数 200 浏览 1 评论 0原文

将元素添加到此列表时如何管理向量的析构函数?当对象超出范围时是否正确销毁?是否存在无法正确删除对象的情况?例如,如果“表”是对象的子对象,并且我们向对象指针向量添加了一个新表,会产生什么后果?

vector <object*> _objectList;

_objectList.PushBack(new object);

How is the destructor for the vector managed when adding elements to this list? Is the object destroyed correctly when it goes out of scope? Are there cases where it would not delete the object correctly? For example what are the consequences if "table" was a child of object, and we added a new table to a vector of object pointers?

vector <object*> _objectList;

_objectList.PushBack(new object);

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

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

发布评论

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

评论(4

眼角的笑意。 2024-08-22 11:55:08

由于您正在创建一个“裸”指针向量,因此 C++ 不可能知道所讨论的指针意味着拥有它们所指向的对象的“所有权”,因此当指针消失。您应该使用简单的“智能”指针而不是“裸”指针作为向量的项目。例如,Boost 的 shared_ptr 就完全足够了(尽管你当然可以使用“更便宜”、更轻量级的方法来完成它,如果你不想将 Boost 作为一个整体来处理并且在代码中不需要智能指针)。

编辑:因为您(OP)说使用诸如Boost之类的框架是不可行的,并且一些评论有用地指出即使包装 std:: auto_ptr 并不真正有资格作为一个像样的快捷方式,您可能必须实现自己的智能指针(或者,如果您找到一个看起来可用的开源、独立的智能指针模板类,请审核它的合规性符合您的要求)。这篇文章是 C++ 中智能指针的有用入门读物,无论您是必须自行推出或审核现有实施。

Since you're making a vector of "bare" pointers, C++ can't possibly know that the pointers in question are meant to have "ownership" of the objects they point to, and so it will not call those objects' destructors when the pointer goes away. You should use a simple "smart" pointer instead of a "bare" pointer as the vector's item. For example, Boost's shared_ptr would be perfectly adequate for the task (although you can surely do it with "cheaper", lighter-weight approaches, if you don't want to deal with Boost as a whole and have no other need for smart pointers in your code).

Edit: since you (the OP) say that using a framework such as Boost is not feasible, and a couple comments usefully point out that even wrapping std::auto_ptr doesn't really qualify as a decent shortcut, you may have to implement your own smart pointers (or, if you find an open-source, stand-alone smart pointer template class that looks usable, audit it for compliance with your requirements). This article is a useful primer to smart pointers in C++, whether you have to roll your own or audit an existing implementation.

享受孤独 2024-08-22 11:55:08

您可以使用 bost 'ptr_vector'。当项目被删除或 ptr_vector 实例超出范围时,它将自动销毁项目所指向的对象。更多信息请访问此处

You could use bost 'ptr_vector'. It will automatically destruct objects that the items point to when they are either deleted or the instance of ptr_vector goes out of scope. More info is available here.

如梦 2024-08-22 11:55:08

在您的情况下,对象指针被正确销毁,但实际对象本身不会被触及。 STL 正确地破坏所有包含的元素 - 但不会隐式取消引用类型指针。

In your case, the object pointers are destroyed properly, but the actual objects themselves won't be touched. The STL properly destructs all contained elements - but will not implicitly dereference pointers to types.

葮薆情 2024-08-22 11:55:08

STL Vectors 会复制您放入其中的任何内容,并最终删除该副本。

因此,在这种情况下,向量存储的是指向对象的指针,而不是对象本身。因此它复制了指针,并删除了该指针。但是,正如克里斯所说< /a>,对象本身不会被删除。

所以,解决方案:

如果你真的不需要使用指针,那么就不要:

vector <object> _objectList;
_objectList.PushBack(object());

如果你确实需要使用指针,你可以使用智能指针(它为你处理引用计数,并将删除对象以及指针),如 Alex建议,或使用 ptr_vector,正如伊戈尔提到的

STL Vectors make a copy of whatever you put in there, and ultimately delete that copy.

So in this case, the vector is storing a pointer to an object - not the object itself. So it makes a copy of the pointer, and deletes the pointer. But, as Chris said, the object itself will not be deleted.

So, solutions:

If you don't really need to use pointers, then don't:

vector <object> _objectList;
_objectList.PushBack(object());

If you do need to use pointers, you can either use a smart pointer (which handles reference counting for you, and will delete the object along with the pointer), as Alex suggested, or use a ptr_vector, as Igor mentioned.

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