在linux上,std :: deque在程序退出之前不会释放内存.完整的代码如下.任何帮助将不胜感激!
#include <deque>
#include <vector>
#include <string>
#include <iostream>
#include <boost/shared_ptr.hpp>
#include <queue>
#include <list>
#include <cstdio>
#include <cstdlib>
typedef boost::shared_ptr<std::vector<int> > VecPtr;
typedef std::deque< VecPtr > QueueType;
char buf[1024];
char line[1024];
int main()
{
{
int v=0;
QueueType deq;
for(int i=0; i<30;++i)
for(int j=0; j<1000;++j)
for(int k=0;k<1000;++k)
{
VecPtr p( new std::vector<int>);
deq.push_back(p);
}
std::cout<<"Done with increasing deq:deq size="<<deq.size()<<std::endl;
sleep(20);
std::cout<<"start decreasing deq size"<<std::endl;
while(deq.size()>0)
{
deq.pop_front();
}
std::cout<<"done with decreasing deq size,deq size="<<deq.size()<<std::endl;
}
std::cin.getline(line,sizeof(line));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Cub*_*bbi 18
这是正确的,pop_front()不释放由分配的存储push_back()
如果要在程序结束之前解除分配,则可以结束对象的生命周期.如果要在对象的生命周期结束之前解除分配,请考虑对C++容器类使用" 缩小到适合 "的习惯用法.
QueueType().swap (deq); // C++98
deq.shrink_to_fit(); // C++11
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3773 次 |
| 最近记录: |