c++ - std::queue 内存消耗导致内存泄漏 - C++?

标签 c++ visual-c++ memory stl queue

即使在我从 qInt 队列中弹出所有元素后,以下代码也没有释放 3000 个元素消耗的内存。是什么原因 ?

std::queue<int> qInt; //Step01: Check the running memory

for (int i=0;i<3000;i++)
{       
    qInt.push(i);
}
//Step02: Check the running memory it should have been increased    

while(!qInt.empty())
{
    qInt.pop();
}
//Step03: Check the running memory expecting Step01 memory but it is still the same of Step02

最佳答案

默认情况下,std 容器在保留内存后不会释放内存。 std::queue 通常在提供 shrink_to_fit 的 std::dequeue 类型上实现。 .如果您不使用 C++ 11,请使用 swap idiom .

关于c++ - std::queue 内存消耗导致内存泄漏 - C++?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21964251/

相关文章:

android - 具有静态方法的类在 Android 中存在多长时间?

iphone - 返回指针后 NSArray 对象超出范围

c++ - 目标文件中未解析的外部符号

c++ - ATL(atlalloc.h) 在编译时产生错误

c - 增加结构段错误的成员

c++ - 实例化结构模板时出现问题

visual-c++ - 从 CStringArray 的元素获取可修改的 TCHAR*

c++ - 错误 : The imported project "C:\Program Files (x86)\MSBuild\Microsoft\WindowsXaml\v11.0\Microsoft.Windows.UI.Xaml.Cpp.targets" was not found

c++ - 内存泄漏能走多远?

c++ - 将非英文字符串存储在 std::string 中