这是 c++ 中的有效删除操作吗?

发布于 2024-11-30 11:42:01 字数 176 浏览 1 评论 0原文

我有一个向量array 在我的代码中,每当我想向我的向量添加一些对象时,我都会使用 array.push_back(*new some_struct)。现在我想知道在清除数组之前是否应该删除数组中的每个对象(使用delete &array[i])?

I have a vector<some_struct&> array in my code, and whenever I want to add some object to my vector I use array.push_back(*new some_struct). now I'm wondering if I should delete every object in my array before clearing my array (using delete &array[i]) or not?

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

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

发布评论

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

评论(5

何处潇湘 2024-12-07 11:42:01

向量> array 无效,期间。

用于实例化标准库容器的类型必须是对象类型。引用类型(如 some_struct&)不是对象类型。

根据定义,“容器是存储其他对象的对象”(来自 C++03 和 C++0x 中的 §23.1/1)。引用不是对象。

如果您使用不满足容器强加的要求的类型实例化容器,则行为是未定义的:您的代码可能会也可能不会编译,如果编译了,谁知道结果会是什么;什么事情都可能发生..

vector<some_struct&> array is invalid, period.

The type (or types) with which you instantiate a Standard Library container must be object types. A reference type (like some_struct&) is not an object type.

By definition, "containers are objects that store other objects" (from §23.1/1 in both C++03 and C++0x). References are not objects.

The behavior is undefined if you instantiate a container with a type that does not meet the requirements that the container imposes: your code may or may not compile and if it does compile, who knows what the result will be; anything could happen..

心头的小情儿 2024-12-07 11:42:01

这是如何编译的?在我过去 5 年使用过的任何编译器上,这都不是合法的 C++ 代码。

How does this even compile? That's not legal C++ code on any compiler I've used in the past 5 years.

江南烟雨〆相思醉 2024-12-07 11:42:01

向量参考说push_back将数据复制到容器中。在您的示例中,不会复制指向的指针,而是复制每个结构的内容。因此,如果您使用 *new my_struct,在使用它们将数据传递到容器后,对为结构分配的内存的引用将会丢失,这意味着您需要将这些指针存储到您分配的每个结构中,以便能够释放它们,否则会出现内存泄漏。

如果你想保留指针,容器应该是一个 vector,你可以传递 new some_struct 并且应该(例如)用 释放它们>删除数组[i]

The vector reference says that push_back copies the data to the container. In your example, not the pointer to, but the contents of each structure would be copied. So if you use *new my_struct, the reference to the memory allocated for the structs will be lost after having used them to pass the data to the container, meaning you need to store those pointers to each of the structs you allocated somewhere to be able to release them, or you will get memory leaks.

If you wanted to keep the pointers, the container should be a vector<some_struct*>, and you could pass new some_struct and should (e.g.) release them with delete array [i].

相权↑美人 2024-12-07 11:42:01

如果您的向量被声明为 vectorarrayarray.push_back(new some_struct) 不应该工作。 array.push_back(some_struct()) 将起作用,在这种情况下您不需要调用删除。

If your vector is declared vector<some_struct&> array, array.push_back(new some_struct) shouldn't work. array.push_back(some_struct()) will work and in that case you don't need to call delete.

写下不归期 2024-12-07 11:42:01

每次执行此操作时,您都会丢失分配的内存。您本质上是在创建该数据的副本,但一旦此过程完成,原始数据就会丢失在内存中的某个位置。要么将外部引用保留在复制后可以删除的位置,要么使用指针向量,而不是引用。

You're going to lose the memory allocated every time you do this. You're essentially creating a copy of that data, but the original is lost in memory somewhere once this process completes. Either keep an external reference to it somewhere which you can delete after you copy it, or use a vector of pointers, not references.

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