c++删除矢量,对象,*内存

时间:2022-10-10 21:18:15

I am totally confused with regards to deleting of things in C++ If I declare an array of objects and if I use the clear() function. Can I be sure that the memory was released?

如果我声明一个对象数组,如果我使用clear()函数,那么在c++中删除东西,我就完全搞不清了。我能确定记忆被释放了吗?

For example :

例如:

tempObject obj1;
tempObject obj2;
vector<tempObject> tempVector;

tempVector.pushback(obj1);
tempVector.pushback(obj2);

Can I safely call clear to free up all the memory? Or do I need to iterate through to delete one by one?

我可以安全地打电话给clear来释放所有的记忆吗?还是我需要一个一个地重复删除?

tempVector.clear();

If this scenario is changed to a pointer of objects, will the answer be the same as above?

如果这个场景被更改为一个对象指针,答案是否与上面相同?

vector<tempObject> *tempVector;
//push objects....
tempVector->clear();

4 个解决方案

#1


68  

You can call clear, and that will destroy all the objects, but that will not free the memory. Looping through the individual elements will not help either (what action would you even propose to take on the objects?) What you can do is this:

您可以调用clear,它将销毁所有对象,但这不会释放内存。循环遍历各个元素也没有帮助(您甚至会建议对对象采取什么行动?)你能做的是:

vector<tempObject>().swap(tempVector);

That will create an empty vector with no memory allocated and swap it with tempVector, effectively deallocating the memory.

这将创建一个没有分配内存的空向量,并将其与tempVector交换,从而有效地释放内存。

C++11 also has the function shrink_to_fit, which you could call after the call to clear(), and it would theoretically shrink the capacity to fit the size (which is now 0). This is however, a non-binding request, and your implementation is free to ignore it.

c++ 11也有shrink_to_fit函数,您可以在调用clear()之后调用它,它理论上会缩小容量以适应大小(现在是0)。

#2


18  

There are two separate things here:

这里有两个不同的东西:

  1. object lifetime
  2. 对象生命周期
  3. storage duration
  4. 存储时间

For example:

例如:

{
    vector<MyObject> v;
    // do some stuff, push some objects onto v
    v.clear(); // 1
    // maybe do some more stuff
} // 2

At 1, you clear v: this destroys all the objects it was storing. Each gets its destructor called, if your wrote one, and anything owned by that MyObject is now released. However, vector v has the right to keep the raw storage around in case you want it later.

在1点,清除v:这会破坏它存储的所有对象。每一个都被调用它的析构函数,如果你写了一个,那么MyObject的所有东西现在都被释放了。但是,向量v有权保留原始存储空间,以备稍后需要。

If you decide to push some more things into it between 1 and 2, this saves time as it can reuse the old memory.

如果您决定在1和2之间插入更多的东西,这将节省时间,因为它可以重用旧的内存。

At 2, the vector v goes out of scope: any objects you pushed into it since 1 will be destroyed (as if you'd explicitly called clear again), but now the underlying storage is also released (v won't be around to reuse it any more).

在2时,向量v超出了范围:从1开始,你插入到它的任何对象都将被销毁(就像你再次明确地调用clear一样),但是现在底层的存储也被释放了(v将不再需要再使用它了)。


If I change the example so v becomes a pointer, you need to explicitly delete it, as the pointer going out of scope at 2 doesn't do that for you. It's better to use something like std::unique_ptr in that case, but if you don't and v is leaked, the storage it allocated will be leaked as well. As above, you need to make sure v is deleted, and calling clear isn't sufficient.

如果我改变例子v变成一个指针,你需要显式地删除它,因为指针在2超出作用域时不会这样做。在这种情况下,最好使用std: unique_ptr之类的东西,但是如果不这样做并且v被泄漏,那么它分配的存储也将被泄漏。如上所述,您需要确保v被删除,并且调用clear是不够的。

#3


14  

vector::clear() does not free memory allocated by the vector to store objects; it calls destructors for the objects it holds.

向量::clear()不释放向量分配给存储对象的内存;它为它持有的对象调用析构函数。

For example, if the vector uses an array as a backing store and currently contains 10 elements, then calling clear() will call the destructor of each object in the array, but the backing array will not be deallocated, so there is still sizeof(T) * 10 bytes allocated to the vector (at least). size() will be 0, but size() returns the number of elements in the vector, not necessarily the size of the backing store.

例如,如果向量使用数组作为后备存储器,目前包含10个元素,然后调用清晰()将调用数组中的每个对象的析构函数,但支持数组将不会被收回,所以还有sizeof(T)* 10个字节分配向量(至少)。size()将为0,但是size()返回向量中元素的数量,不一定是后台存储的大小。

As for your second question, anything you allocate with new you must deallocate with delete. You typically do not maintain a pointer to a vector for this reason. There is rarely (if ever) a good reason to do this and you prevent the vector from being cleaned up when it leaves scope. However, calling clear() will still act the same way regardless of how it was allocated.

至于第二个问题,使用new分配的任何内容都必须使用delete来处理。由于这个原因,通常不维护指向向量的指针。很少(如果有的话)有一个很好的理由去做这个,并且当它离开作用域时,你阻止了这个向量被清理。但是,无论如何分配clear(),调用clear()仍然以相同的方式执行。

#4


0  

If you need to use the vector over and over again and your current code declares it repeatedly within your loop or on every function call, it is likely that you will run out of memory. I suggest that you declare it outside, pass them as pointers in your functions and use:

如果您需要一次又一次地使用这个矢量,并且您的当前代码在您的循环或每个函数调用中多次声明它,那么您可能会耗尽内存。我建议您在外部声明它,在函数中作为指针传递它们,并使用:

my_arr.resize()

This way, you keep using the same memory sequence for your vectors instead of requesting for new sequences every time. Hope this helped. Note: resizing it to different sizes may add random values. Pass an integer such as 0 to initialise them, if required.

这样,您就可以一直使用相同的内存序列,而不是每次都请求新的序列。希望这个有帮助。注意:将它调整到不同的大小可以添加随机值。如果需要,传递一个整数,如0,以初始化它们。

#1


68  

You can call clear, and that will destroy all the objects, but that will not free the memory. Looping through the individual elements will not help either (what action would you even propose to take on the objects?) What you can do is this:

您可以调用clear,它将销毁所有对象,但这不会释放内存。循环遍历各个元素也没有帮助(您甚至会建议对对象采取什么行动?)你能做的是:

vector<tempObject>().swap(tempVector);

That will create an empty vector with no memory allocated and swap it with tempVector, effectively deallocating the memory.

这将创建一个没有分配内存的空向量,并将其与tempVector交换,从而有效地释放内存。

C++11 also has the function shrink_to_fit, which you could call after the call to clear(), and it would theoretically shrink the capacity to fit the size (which is now 0). This is however, a non-binding request, and your implementation is free to ignore it.

c++ 11也有shrink_to_fit函数,您可以在调用clear()之后调用它,它理论上会缩小容量以适应大小(现在是0)。

#2


18  

There are two separate things here:

这里有两个不同的东西:

  1. object lifetime
  2. 对象生命周期
  3. storage duration
  4. 存储时间

For example:

例如:

{
    vector<MyObject> v;
    // do some stuff, push some objects onto v
    v.clear(); // 1
    // maybe do some more stuff
} // 2

At 1, you clear v: this destroys all the objects it was storing. Each gets its destructor called, if your wrote one, and anything owned by that MyObject is now released. However, vector v has the right to keep the raw storage around in case you want it later.

在1点,清除v:这会破坏它存储的所有对象。每一个都被调用它的析构函数,如果你写了一个,那么MyObject的所有东西现在都被释放了。但是,向量v有权保留原始存储空间,以备稍后需要。

If you decide to push some more things into it between 1 and 2, this saves time as it can reuse the old memory.

如果您决定在1和2之间插入更多的东西,这将节省时间,因为它可以重用旧的内存。

At 2, the vector v goes out of scope: any objects you pushed into it since 1 will be destroyed (as if you'd explicitly called clear again), but now the underlying storage is also released (v won't be around to reuse it any more).

在2时,向量v超出了范围:从1开始,你插入到它的任何对象都将被销毁(就像你再次明确地调用clear一样),但是现在底层的存储也被释放了(v将不再需要再使用它了)。


If I change the example so v becomes a pointer, you need to explicitly delete it, as the pointer going out of scope at 2 doesn't do that for you. It's better to use something like std::unique_ptr in that case, but if you don't and v is leaked, the storage it allocated will be leaked as well. As above, you need to make sure v is deleted, and calling clear isn't sufficient.

如果我改变例子v变成一个指针,你需要显式地删除它,因为指针在2超出作用域时不会这样做。在这种情况下,最好使用std: unique_ptr之类的东西,但是如果不这样做并且v被泄漏,那么它分配的存储也将被泄漏。如上所述,您需要确保v被删除,并且调用clear是不够的。

#3


14  

vector::clear() does not free memory allocated by the vector to store objects; it calls destructors for the objects it holds.

向量::clear()不释放向量分配给存储对象的内存;它为它持有的对象调用析构函数。

For example, if the vector uses an array as a backing store and currently contains 10 elements, then calling clear() will call the destructor of each object in the array, but the backing array will not be deallocated, so there is still sizeof(T) * 10 bytes allocated to the vector (at least). size() will be 0, but size() returns the number of elements in the vector, not necessarily the size of the backing store.

例如,如果向量使用数组作为后备存储器,目前包含10个元素,然后调用清晰()将调用数组中的每个对象的析构函数,但支持数组将不会被收回,所以还有sizeof(T)* 10个字节分配向量(至少)。size()将为0,但是size()返回向量中元素的数量,不一定是后台存储的大小。

As for your second question, anything you allocate with new you must deallocate with delete. You typically do not maintain a pointer to a vector for this reason. There is rarely (if ever) a good reason to do this and you prevent the vector from being cleaned up when it leaves scope. However, calling clear() will still act the same way regardless of how it was allocated.

至于第二个问题,使用new分配的任何内容都必须使用delete来处理。由于这个原因,通常不维护指向向量的指针。很少(如果有的话)有一个很好的理由去做这个,并且当它离开作用域时,你阻止了这个向量被清理。但是,无论如何分配clear(),调用clear()仍然以相同的方式执行。

#4


0  

If you need to use the vector over and over again and your current code declares it repeatedly within your loop or on every function call, it is likely that you will run out of memory. I suggest that you declare it outside, pass them as pointers in your functions and use:

如果您需要一次又一次地使用这个矢量,并且您的当前代码在您的循环或每个函数调用中多次声明它,那么您可能会耗尽内存。我建议您在外部声明它,在函数中作为指针传递它们,并使用:

my_arr.resize()

This way, you keep using the same memory sequence for your vectors instead of requesting for new sequences every time. Hope this helped. Note: resizing it to different sizes may add random values. Pass an integer such as 0 to initialise them, if required.

这样,您就可以一直使用相同的内存序列,而不是每次都请求新的序列。希望这个有帮助。注意:将它调整到不同的大小可以添加随机值。如果需要,传递一个整数,如0,以初始化它们。