std :: queue内存消耗导致内存泄漏-C ++? [英] std::queue memory consumption leads to memory leak - C++ ?

查看:888
本文介绍了std :: queue内存消耗导致内存泄漏-C ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The following codes didn't release the memory consumed for 3000 elements even after i pops out all the elements from the qInt queue. What is the reason ?

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

推荐答案

通过defalut std容器,一旦保留了内存,就不会释放内存.通常在提供 shrink_to_fit .如果您不使用c ++ 11,请使用交换习惯.

By defalut std containers do not deallocate memory once they have reserved it. The std::queue is generally implemented on type of std::dequeue which offers shrink_to_fit. If you are not using c++ 11 use the swap idiom.

这篇关于std :: queue内存消耗导致内存泄漏-C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆