测试过程:
insert采用malloc分配内存,释放时采用free释放内存,并erase操作子。使用top窗口查看程序使用的内存。
测试结果:
free后,操作系统为程序分配的内存并不会free掉,而被Hold了。当再次调用insert入map,会使用未free掉的内存。
如果后面insert同样采用malloc分配内存,操作系统会优先使用先去malloc且free掉的内存,若不够再malloc新内存。
测试代码如下:
#include <iostream>
#include <map>
#include <ext/hash_map>
using namespace std;
using namespace __gnu_cxx;
#pragma pack(1)
typedef struct
{
uint32_t uiUserId; //Systematic distribution id for every UserPhone;
uint8_t ucFlag; //first send flag:0,no send;1,send
uint8_t ucAlter; //user can send msg:0,no;1,yes
char szUserPhone[14]; //user phone number
uint32_t uiLastestBscID;
uint32_t uiLastestAreaID;
time_t uiMonthBeginTime; //cur month send num begin time
time_t uiLastestSendTime; //last send welcome sms time
time_t uiFirstSendTime; //first send welcome sms time
uint16_t usDaySendNum; //send count every day
uint16_t usMonthSendNum; //send count every month
uint16_t usTotal; //send total count
uint32_t uiAreaList[5];
}CUserInFile;
#pragma pack()
#define LEN sizeof(CUserInFile)
typedef map<int,CUserInFile*> UserMap;
UserMap userMap;
int main()
{
FILE *fp;
char buff[LEN];
int i=0;
fp=fopen("userprofile_wifi","r");
memset(buff,0x00,LEN);
while(fread(buff,1,LEN,fp)==LEN)
{
fseek(fp,SEEK_CUR,LEN);
//fwrite(buff,LEN,10,fpw);
CUserInFile* pUser=(CUserInFile*)malloc(LEN);
userMap.insert(make_pair(i++,pUser));
}
fclose(fp);
//fclose(fpw);
printf("insert ok:%d\n",i);
getchar();
UserMap::iterator it;
for(it=userMap.begin();it!=userMap.end();++it)
{
if(it->second==NULL)
{
printf("dasdsadas\n");
continue;
}
CUserInFile* pUser=it->second;
free(pUser);
userMap.erase(it);
}
printf("free ok\n");
getchar();
fp=fopen("userprofile_wifi","r");
memset(buff,0x00,LEN);
while(fread(buff,1,LEN,fp))
{
fseek(fp,SEEK_CUR,LEN);
CUserInFile* pUser=(CUserInFile*)malloc(LEN);
userMap.insert(make_pair(i++,pUser));
}
fclose(fp);
printf("insert again ok\n");
getchar();
return 0;
}
后面测试了HASH_MAP,结论类似。
分享到:
相关推荐
学习STL MAP, STL SET之数据结构基础
STL中map用法详解 STL中map用法详解 STL中map用法详解
STL map例子 基本操作包括增删改查!
stl map algorithm ppt 精品课件
stl中map用法详解
可以在windbg导出stl map和set的插件,使用方法参考我csdn的博客http://blog.csdn.net/yichigo/article/details/38232511
C++11中有无序map即: unordered_map 可以不自动排序, 那么C++98中只有map 如何避免自动排序。
STL中map用法详解 非常详细,有说明和可以编译的例子
vector list map pair stl 标准模板库 c++ 程序示例
基于STL的内存池类 和 测试程序 在linux下已做过稳定性测试
C++ stl中map容器的基本概念与增删查改功能实现
基于stl共享内存,可以像使用STL容器一样使用共享内存。方便快捷。具体参考里面的代码实现
实现统计一段文章的每个单词的个数...其中CountDemo使用STL中的Map来实现的 CountDemo2是用一般语言实现,没有用到STL实现的; MapCount是用STL中的Vector和Map共同实现的 此题目是学习STL中的Map和Vector必练的经典题目
如果你具备一定的C++ template知识,即使你没有接触过STL,这个文章你也应该可能较轻易的看懂。
自己编写了STL库的map,供大家学习讨论
以STL::map实现引用表,如有错误,欢迎指教!
STL中map用法详解STL中map用法详解STL中map用法详解
STL的map例子agtbouns STL的map例子agtbouns
stl_map,讲述stlmap的一篇!大家可以学学!
一个学习STL 中Map的类,主要实现stl::map的查找、删除、增加等