简体   繁体   English

对象向量的C ++内存管理

[英]C++ Memory management for vector of Objects

I have a few questions/examples about how memory management works for Vectors. 我有一些关于Vector的内存管理工作方式的问题/示例。

    vector<int> vec1(10);
    vector<int> vec2(10);        
    vec1 = vec2;

In a case like this, vec1's old memory is unreachable now. 在这种情况下,vec1的旧内存现在无法访问。 Is it still a memory leak here or would vec2's old memory realize there nothing referencing it and get cleaned up? 这里是否仍然是内存泄漏,还是vec2的旧内存会发现没有引用它并对其进行清理?

In another example 在另一个例子中

struct foo
{
   vector<int> foo_vec(50);
   int* arr; 
}


void myFunction()
{
   vector<foo> vec(10);
   vec[0].arr = new int[50];
   vec.erase(vec.begin());
}

Since I used erase() on the first vector element which contained arr with the allocated memory, does erase() release that memory or do I need to release it manually prior to the erase? 由于我在分配的内存中包含arr的第一个矢量元素上使用了erase() ,所以erase()释放该内存还是需要在擦除之前手动释放它? Also when vec goes out of scope, are all the foo_vec vectors in vec automatically cleaned up? 此外,当vec超出范围,都是foo_vec在向量vec自动清理? Thanks for any help. 谢谢你的帮助。

Does erase() release that memory or do I need to release it manually prior to the erase? 是否delete()会释放该内存,还是需要在擦除之前手动释放它?

The rule: Always use delete for each new . 规则: 始终对每个new使用delete vector ain't no magic - it doesn't know how you obtained that pointer, so it won't delete it, you need to do it. vector并非没有魔力-它不知道您如何获得该指针,因此它不会删除它,您需要这样做。

Also when vec goes out of scope, are all the foo_vec vectors in vec automatically cleaned up? 另外,当vec超出范围时,vec中的所有foo_vec向量都会自动清除吗?

Yes, their destructor is called and they're deallocated. 是的,它们的析构函数被调用,并且它们已被释放。 However, you will leak memory if the destructor doesn't delete[] arr; 但是,如果析构函数不delete[] arr; ,则会泄漏内存delete[] arr; .


By the way, a piece of good advice: your code violates encapsulation. 顺便提一下,一个好的建议:您的代码违反了封装。 You should allocate ( new ) and free ( delete[] ) the memory pointed to by foo::arr in the destructor and the constructor ( foo::foo() and foo::~foo() ), respectively. 您应该在析构函数和构造函数中分别分配( new )和free( delete[]foo::arr指向的内存( foo::foo()foo::~foo() )。

Finally, the obligatory question: why foo::arr isn't a vector<int> itself? 最后,一个强制性的问题:为什么foo::arr本身不是vector<int>

In the case of 如果是

vector<int> vec1(10);
vector<int> vec2(10);        
vec1 = vec2;

the previous contents of vec1 are erased since int is just a POD. 由于int只是POD,因此会删除vec1的先前内容。 If the vector consisted of pointers to objects instead of ints and these were allocated with new you would have to delete what those pointers point to before you assign, otherwise you would end up with memory leaks which your second example shows: 如果vector由指向对象的指针而不是整数组成,并且使用new指针分配,则在分配之前必须删除这些指针指向的对象,否则将导致内存泄漏,第二个示例显示:

vector<foo> vec(10);
vec[0].arr = new int[50];
vec.erase(vec.begin());    // this will *not* call delete on vec[0].arr

Normally what you do to make things simpler for yourself is to use smart pointers like unique pointers (or eg boost::shared_array/scoped_array). 通常,使自己更简单的方法是使用智能指针(例如唯一指针)(例如boost :: shared_array / scoped_array)。 When the vector goes out of scope or you erase the vector the delete (or delete []) will be automatically called. 当向量超出范围或删除向量时,将自动调用删除(或delete [])。

struct foo
{
   std::vector<int> foo_vec(50);
   std::unique_ptr<int[]> arr; 
}
...
vec[0].arr.reset(new int[50]);
vec[0].arr[12] = 42;
...
vec.erase(vec.begin());    // this will call delete[] on vec[0].arr
  1. Of course it will be cleaned. 当然会被清洗。
  2. No, if destructor of foo doesn't do this (or if you don't do it manually before erase). 否,如果foo destructor不执行此操作(或者在擦除前不手动执行此操作)。

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

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