问题描述
假设我有一个std :: vector的structs。如果向量是clear()'d?
Suppose I have a std::vector of structs. What happens to the memory if the vector is clear()'d?
std::vector<myStruct> vecs;
vecs.resize(10000);
vecs.clear();
内存是否被释放,还是连接到vecs变量作为可重用缓冲区?
Will the memory be freed, or still attached to the vecs variable as a reusable buffer?
推荐答案
内存保持连接到向量。如果你想释放它,通常是与一个空的向量交换。 C ++ 11还添加了 shrink_to_fit
成员函数旨在更直接地提供大致相同的功能,但它是非绑定的(换句话说,它可能释放额外的内存,但仍然不是真正需要这样做)。
The memory remains attached to the vector. If you want to free it, the usual is to swap with an empty vector. C++11 also adds a shrink_to_fit
member function that's intended to provide roughly the same capability more directly, but it's non-binding (in other words, it's likely to release extra memory, but still not truly required to do so).
这篇关于是std :: vector内存释放清楚吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!