map的erase()释放内存
STL中的map调用erase(it),当value值为指针时,释放内存:
#include <iostream>
#include <map>
#include <string> using namespace std;
struct value{
int i;
std::string test;
}; int main()
{
std::map<int, value*> test_map;
for(int i=; i<; ++i){
value* tmp = new value();
tmp->i = i;
tmp->test = "test";
test_map.insert(make_pair(i, tmp));
} for(std::map<int, value*>::iterator it=test_map.begin(); it!=test_map.end();){
std::cout << "first " << it->first << " second " << (it->second)->i <<" "<< (it->second)->test << std::endl;
delete it->second;
it->second = NULL;
//test_map.erase(it); //迭代器失效;
test_map.erase(it++); //防止迭代器失效,切记、切记
} return ;
} root@u18:~/cp/test# g++ map3.cpp -g -Wall
root@u18:~/cp/test# ls -lt a.out
-rwxr-xr-x root root Jul : a.out
root@u18:~/cp/test# valgrind --tool=memcheck --leak-check=full ./a.out
==== Memcheck, a memory error detector
==== Copyright (C) -, and GNU GPL'd, by Julian Seward et al.
==== Using Valgrind-3.7. and LibVEX; rerun with -h for copyright info
==== Command: ./a.out
====
first second test
first second test
first second test
first second test
first second test
first second test
first second test
first second test
first second test
first second test
====
==== HEAP SUMMARY:
==== in use at exit: bytes in blocks
==== total heap usage: allocs, frees, bytes allocated
====
==== All heap blocks were freed -- no leaks are possible
====
==== For counts of detected and suppressed errors, rerun with: -v
==== ERROR SUMMARY: errors from contexts (suppressed: from )
map的erase()释放内存的更多相关文章
- C++ STL map容器值为指针时怎么释放内存
最近在使用STL中map时,遇到了一个问题,就是当map中值为指针对象时怎么释放内存? // 站点与TCP连接映射表 (key为ip_port_stationCode, value为 clientSo ...
- vector容器删除某些元素且释放内存
1,size和capacity size: 指目前容器中实际有多少元素,对应的resize(size_type)会在容器尾添加或删除一些元素,来调整容器中实际的内容,使容器达到指定的大小. capac ...
- CF731C Socks并查集(森林),连边,贪心,森林遍历方式,动态开点释放内存
http://codeforces.com/problemset/problem/731/C 这个题的题意是..小明的妈妈给小明留下了n只袜子,给你一个大小为n的颜色序列c 代表第i只袜子的颜色,小明 ...
- c++ vector 释放内存
1.释放单个指针 关于Vector中存放指针的问题,在进行清空的时候比较安全的一种做法是: std::vector<ClassName *> ClassNameVec; ... ...
- vector释放内存之swap方法
相信大家看到swap这个词都一定不会感到陌生,就是简单的元素交换.但swap在C++ STL中散发着无穷的魅力.下面将详细的说明泛型算法swap和容器中的swap成员函数的使用! 1. 泛型算法swa ...
- 完美删除vector的内容与释放内存
问题:stl中的vector容器常常造成删除假象,这对于c++程序员来说是极其讨厌的,<effective stl>大师已经将之列为第17条,使用交换技巧来修整过剩容量.内存空洞这个名词是 ...
- CentOS7清理yum缓存和释放内存方法
清理yum缓存 清理yum缓存使用yum clean 命令,yum clean 的参数有headers, packages, metadata, dbcache, plugins, expire-ca ...
- 在dll里malloc/new/cvCreate分配内存,在exe里free/Releases释放内存时会出错。
写了个程序,在DLL中用malloc分配了一块内存,但是在exe程序中释放,结果程序crash,原因就是:其原因可能是堆被损坏,这也说明 TestMySticker.exe 中或它所加载的任何 DLL ...
- PHP unset()函数销毁变量 但没有实现释放内存
<?PHP $a = "hello";$b = &$a;unset( $b );echo $a; // 输出 helloecho $b; // 报错$b = &quo ...
随机推荐
- input 属性
1.readonly="readonly" 用来表示 input只能读写.
- CI(-)框架结构
一 CI 是什么 CodeIgniter is an Application Development Framework - a toolkit - for people who build web ...
- php学习笔记(1)
1.引用文件的方法有两种:require 及 include require 的使用方法如 require("MyRequireFile.php"); .这个函数通常放在 PHP ...
- eclipse更改主题
长期使用eclipse,导致视觉疲劳,就想着能否换个主题调节调节. 通过设置window>preferences>appearance设置theme,貌似不起作用. 一查,发现一个绝佳的网 ...
- 动态链接库 DLL
动态链接库DLL 不使用时不会有任何作用,只有在其他模块调用动态链接库中的函数时,它才发挥作用. 一.静态库与动态库 1.静态库 函数和数据被编译进一个二进制文件(.LIB),编译时,会将其组合起来创 ...
- UVA-514 Rails (栈)
Rails There is a famous railway station in PopPush City. Country there is incredibly hilly. The s ...
- java实现 阿拉伯数字转换为汉字数字 算法
package test; public class NumberFormatTest { static String[] units = { "", "十", ...
- MVC Razor中 如何截断字符串
有时候显示的内容过长,使用MVC编程时,如何截断显示的内容呢.我知道你肯定有很多办法这样做的,但是在学习MVC时,还是使用一些新的办法做吧> Razor 标记语法编程. @helper Trun ...
- Node.js初学
Node.js 初学~ 其技术上最大的卖点是非阻塞的I/O和基于事件的异步处理机制. 后端没有什么深入研究,一直对其不是很了解. 透过一个例子看 非阻塞 与 通常的 阻塞 var text = rea ...
- zoj 1409 Communication System
/*如果要一个物体的多种属性,最好用结构体,不要用二维数组或者多维数组.用多维数组进行关键字排序很不稳定 */ /*给每个设备的所有价格排序,每个设备选取恰好比已知带宽大的价格(这个时候的比例最大) ...