这是 c++ 中的有效删除操作吗?
我有一个向量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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
向量> 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..
这是如何编译的?在我过去 5 年使用过的任何编译器上,这都不是合法的 C++ 代码。
How does this even compile? That's not legal C++ code on any compiler I've used in the past 5 years.
向量参考说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 passnew some_struct
and should (e.g.) release them withdelete array [i]
.如果您的向量被声明为
vectorarray
、array.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.每次执行此操作时,您都会丢失分配的内存。您本质上是在创建该数据的副本,但一旦此过程完成,原始数据就会丢失在内存中的某个位置。要么将外部引用保留在复制后可以删除的位置,要么使用指针向量,而不是引用。
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.