为什么需要手动从向量中删除指针? [英] Why do I need to delete pointers from vector manually?

查看:60
本文介绍了为什么需要手动从向量中删除指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么需要手动删除矢量中动态创建的项目?为什么在删除vector时为什么不删除它们或调用其析构函数?

Why do I need to delete dynamically created items in vector, manually? Why wouldn't they get deleted or its destructor called when vector got deleted?

通常是这样,但是为什么需要呢?

Normally something like this, but why needed ?

vector<int*> v;
for (vector<int*>::iterator it = v.begin(); it != v.end(); ++it) 
{ 
   delete *it; 
}

推荐答案

首先,将原始指针存储在向量中.这些指针仅仅是指针.他们可以指向任何地方.它们可以指向本地对象,这些对象不能被delete删除.即使它们指向动态创建的对象,也不一定意味着用户希望它们与矢量一起消亡.向量应该怎么知道所有这些?

Firstly, you store raw pointers in your vector. These pointers are just pointers. They can point anywhere. They can point to local objects, which cannot be deleted by delete. And even if they point to dynamically created objects, it doesn't necessarily mean that the user wants them to die with the vector. How is the vector supposed to know all this?

这是对象所有权的问题.拥有该对象的人有责任对其进行适当,及时的删除.普通的原始指针不表示所有权.这就是为什么vector无法对是否需要删除对象做出任何假设的原因.如果要告诉向量它拥有其动态对象,请使用相应的智能指针.

It is a matter of object ownership. Whoever owns the object is responsible for its proper and timely deletion. Ordinary raw pointers do not express ownership. That is why vector can't make any assumptions about whether the objects need to be deleted or not. If you want to tell the vector that it owns its dynamic objects, use corresponding smart pointers.

第二,请注意,一般情况下,删除技术不一定安全.一些标准容器假定您存储在其中的数据始终有效.当对每个向量元素执行delete时,向量中的数据将变为无效"(指针变为不确定).用向量可以.但是在更智能"的容器中执行类似的操作,例如std::mapstd::unordered_set,可能会并会导致问题.即使您随后立即销毁容器本身,也很可能容器的销毁算法可能需要分析(比较,哈希等)单个元素的值.而您只是将自己杀死了.

Secondly, note that your deletion technique is not necessarily safe in the general case. Some standard containers assume that the data you store in them is always valid. When you do delete on each vector element, the data in the vector becomes "invalidated" (pointers become indeterminate). This is OK with a vector. But doing something like this in a "smarter" container, like std::map or std::unordered_set for example, can and will lead to problems. Even if you destroy the container itself immediately afterwards, it is perfectly possible that the container's destruction algorithm might need to analyze (compare, hash etc.) the values of individual elements. And you just killed them all with your cycle.

智能指针自然可以解决此问题.但是,如果必须对存储在标准容器中的原始指针使用手册delete,则更好的步骤顺序是

Smart pointers naturally resolve this matter. But if you have to use a manual delete for raw pointers stored in a standard container, a better sequence of steps would be this

  1. i检索元素的值并将其存储在指针p
  2. 从容器中删除i处的元素
  3. 执行delete p
  1. Retrieve the value of the element at i and store it in pointer p
  2. Erase the element at i from the container
  3. Do delete p

最后,您得到一个空容器和delete d个数据.同样,您的方法将适用于简单的序列,例如std::vectorstd::list,但不要对有序或散列的序列执行此操作.

In the end you end up with an empty container and deleted data. Again, your approach will work for simple sequences, like std::vector or std::list, but don't do this with ordered or hashed ones.

这篇关于为什么需要手动从向量中删除指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆