C++ 如何正确销毁向量的c++向量并释放内存

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13679635/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:38:00  来源:igfitidea点击:

How to properly destroy c++ vector of vectors and release memory

c++memory-leaksvector

提问by Keenan Z

Possible Duplicate:
How to release std::vector if there is no heap memory

可能的重复:
如果没有堆内存,如何释放 std::vector

c++ noob here

C++ 菜鸟在这里

Suppose I declare a vector of vectors like:

假设我声明了一个向量向量,例如:

vector<vector<vector<double> > > phi_x;

After I'm done using it, how do I destroy it to free up memory?

使用完后,如何销毁它以释放内存?

I've read some other sites and they recommend using .clear(). From my understanding, this only deletes the elements but the vector capacity is not released. I want the vector to be completely forgotten like it never existed.

我读过其他一些网站,他们推荐使用 .clear()。根据我的理解,这只会删除元素,但不会释放向量容量。我希望向量被完全遗忘,就像它从未存在过一样。

Also, my current application of the vector has dimensions of 54x54x9 and I've got about 12 of these things. Is that considered stupidly big?

此外,我目前对向量的应用具有 54x54x9 的尺寸,我有大约 12 个这样的东西。这被认为是愚蠢的大吗?

回答by melpomene

You don't have to do anything. Just let the variable go out of scope.

你不必做任何事情。只需让变量超出范围即可。

回答by bdonlan

If you call .clear(), the inner vectors will be completely be destroyed. However, the outermost vector may retain the memory it used for its array of vectors (phi_x.size() * sizeof(vector<vector<double> >)bytes). To fully clear it and release all memory, swap with an empty vector:

如果调用.clear(),内部向量将被完全破坏。但是,最外面的向量可能会保留它用于其向量数组(phi_x.size() * sizeof(vector<vector<double> >)字节)的内存。要完全清除它并释放所有内存,请使用空向量交换:

phi_x.swap(vector<vector<vector<double> > >());

Or simply let phi_xgo out of scope.

或者干脆放手phi_x超出范围。

回答by B?ови?

After I'm done using it, how do I destroy it to free up memory?

使用完后,如何销毁它以释放内存?

Just let it go out of scope.

只是让它超出范围。

my current application of the vector has dimensions of 54x54x9 and I've got about 12 of these things. Is that considered stupidly big?

我当前应用的向量的尺寸为 54x54x9,我有大约 12 个这样的东西。这被认为是愚蠢的大吗?

It depends. If it is for an embedded platform with 64k memory, then yes, it is big. If it is for standard desktop PCs, with 4G+ RAM, then it can be neglected.

这取决于。如果是针对具有 64k 内存的嵌入式平台,那么是的,它很大。如果是标准台式机,4G+内存的话,可以忽略不计。

回答by jcoder

You don't have to do anything, the destructor will clean up properly.

你不必做任何事情,析构函数会正确清理。

If you want to free the memory usage before it goes out of scope then you can cal clear on the top level. This will not reallocate the memory used by the top vector but it will remove all the contained vectors and they will release their memory in their destructors.

如果您想在超出范围之前释放内存使用量,那么您可以在顶层调用 clear 。这不会重新分配顶部向量使用的内存,但会删除所有包含的向量,并且它们将在其析构函数中释放内存。

That is about 1 million elements of type double if I calculate it right. Assuming a modern PC as a platform that isn't stupidly big. Or even very big :)

如果我计算正确的话,大约有 100 万个 double 类型的元素。假设一个现代 PC 作为一个平台,它不是愚蠢的大。甚至非常大:)

回答by Agentlien

If this vector as shown by your example, there is no need to do any additional cleaning up, memory will be properly freed by the destructor as soon as it goes out of scope.

如果此向量如您的示例所示,则无需进行任何额外的清理,一旦超出范围,析构函数就会正确释放内存。

This will happen as soon as the function it is declared within exits or if the object it lives in is destroyed.

一旦它在出口中声明的函数或者它所在的对象被销毁,就会发生这种情况。

回答by Jakob

The destructor will do the job of cleaning up for you. You don't have to do anything but let phi_x go out of scope.

析构函数将为您完成清理工作。您无需执行任何操作,只需让 phi_x 超出范围即可。

{
    vector<vector<vector<double> > > phi_x;

    // do stuff with phi_x
}
// phi_x is gone here

回答by Basile Starynkevitch

If your phi_xvariable is some local variable inside some block, it will be destroyed by having its destructor called at end of block.

如果您的phi_x变量是某个块内的某个局部变量,它将通过在块的末尾调用其析构函数来销毁它。

If it is a global variable, it would be destoyed after mainended.

如果它是一个全局变量,它会在main结束后被销毁。