简体   繁体   English

删除c向量的int向量数组

[英]deleting c-style array of int vectors

I have declared: 我宣布:

vector<int> * part = new vector<int>[indmap];

Ok, I know it: “why don't you declare a vector of vector?”. 好吧,我知道:“你为什么不宣布一个矢量向量?”。 The next time I will do so, but now I would like to understand something more about vectors which I'm not able to solve. 下次我会这样做,但现在我想了解更多关于我无法解决的向量的问题。

Now I want to release all resources, what have I to do? 现在我想释放所有资源,我该做什么?

delete[] part;  

But before deleting the array what should I do to delete all the vector objects? 但在删除数组之前,我该怎么做才能删除所有的矢量对象?

No, you don't. 不,你没有。 There's just one simple rule (when you do not use smart pointers ) - use delete when you have used new . 这里有一个简单的规则(当你使用智能指针 ) - 使用delete当您使用new

So, there's just one new[] here, you need just one delete[] . 所以,这里只有一个new[] ,你只需要一个delete[]

@Joachim Pileborg is right, but then you'll have more new -s, so, you'll need more delete -s. @Joachim Pileborg是对的,但是你会有更多的new ,所以,你需要更多的delete

It depends on the contents of the vector. 这取决于矢量的内容。 If it contains raw pointer (like int * ) you need to iterate the vector and delete all entries manually. 如果它包含原始指针(如int * ),则需要迭代向量并手动删除所有条目。 Otherwise, if it contains just basic types, or objects (not pointers to them) or smart pointers it will be handled automatically. 否则,如果它只包含基本类型或对象(不是它们的指针)或智能指针 ,它将自动处理。

When you use delete , the destructor of the pointed-to object is called before de-allocation (do this, when you have created an object using new ). 使用delete ,在取消分配之前调用指向对象的析构函数(执行此操作时,使用new创建对象时)。

Similarly, the destructors of all created objects are called when you use the array version delete[] (do this, when you have allocated an array of objects using new[] ). 类似地,当您使用数组版本delete[]时,将调用所有已创建对象的析构函数(当您使用new[]分配了一个对象数组时,请执行此操作)。

The destructor of a std::vector<T> automatically calls the destructors of the contained objects. std::vector<T>的析构函数自动调用包含对象的析构函数。

So, as Joachim Pileborg has pointed out in his answer, you need to take care of deleting the vector's objects manually, only when you have raw pointers in there that point to dynamically allocated objects (eg, objects allocated by new ) and you are responsible for their deletion (ie, you own them). 因此,正如Joachim Pileborg在他的回答中指出的那样,你需要手动删除向量的对象,只有当你有指向动态分配对象的原始指针(例如,由new分配的对象)并且你负责删除(即你拥有它们)。 Raw pointers don't have destructors that would destroy the pointed-to objects, so you would have to iterate the vector to delete the objects manually in this case. 原始指针没有会破坏指向对象的析构函数,因此在这种情况下,您必须迭代向量以手动删除对象。 Otherwise the vector can simply be destroyed (or array-deleted, in this case). 否则,可以简单地销毁向量(或者在这种情况下删除数组)。

As a rule of thumb: 根据经验:

  • You need to delete memory you have allocated using new . 您需要delete使用new分配的内存。
  • You need to delete[] memory you have allocated using new[] . 您需要使用new[] delete[]已分配的delete[]内存。
  • You can pass this obligation to smart pointers, ie, objects that implement an ownership policy that will take care of the proper deallocation. 您可以将此义务传递给智能指针,即实现所有权策略的对象,该策略将负责正确的解除分配。
  • You should avoid dynamic memory allocation where ever possible and prefer schemes like RAII for memory management. 您应该尽可能避免动态内存分配,并且更喜欢RAII等方案进行内存管理。

For reference: 以供参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM