map的内存分配机制分析
该程序演示了map在形成的时候对内存的操作和分配。
因为自己对平衡二叉树的创建细节理解不够,还不太明白程序所显示的日志。等我明白了,再来修改这个文档。
/*
功能说明:
map的内存分配机制分析。
代码说明:
map所管理的内存地址可以是不连续的。如果key是可以通过<排序的,那么,map最后的结果是有序的。它是通过一个平衡二叉树来保存数据。所以,其查找效率极高。
实现方式: 限制条件或者存在的问题:
无
*/
#include <iostream>
#include <string>
#include <map> #include <windows.h> using namespace std; class CData
{
public:
CData()
{
sequence = ;
this->remark = "default string"; cout << "CData()\t" << toString() << "\t" << this << endl;
} CData(int i, string &s)
{
this->sequence = i;
this->remark = s; cout << "CData(int i,string &s)\t" << toString() << "\t" << this << endl;
} CData(const CData &data)
{
this->sequence = data.sequence;
this->remark = data.remark; cout << "CData(const CData &data)\t" << toString() << "\t" << this << endl;
} CData operator = (const CData &data)
{
this->sequence = data.sequence;
this->remark = data.remark; cout << "CData operator = (const CData &data)\t" << toString() << "\t" << this << endl; return *this;
} void setSequence(const int i)
{
this->sequence = i;
} void setRemark(const string &s)
{
this->remark = s;
} string toString() const
{
char tmp[] = { };
sprintf(tmp, "[sequence:%d | remark:%s]", this->sequence, this->remark.c_str()); //此处应该有内存复制操作,所以不用担心返回后tmp数组所指向的内存被修改或者不存在的情况。
return tmp;
} ~CData()
{
cout << "~CData()\t" << this << endl;
}
protected:
private:
int sequence;
string remark;
}; int main(int argc, char **argv)
{
cout << "process begin at " << (void*)&main << endl; string str = "baby_test";
CData data1(, str);
CData data2(, str);
CData data3(, str);
CData data4(, str);
CData data5(, str);
CData data6(, str);
CData data7(, str);
CData data8(, str);
CData data9(, str);
CData data10(, str); cout << "push CData to map" << endl;
map<int,CData> map_data;
cout << "max size of list<CData> is " << map_data.max_size() << endl;
cout << "size of list<CData> is " << map_data.size() << endl; cout << "****************map_data.push_back(data1)" << endl;
map_data.insert(make_pair(, data1));
Sleep( * map_data.size()); cout << "****************map_data.push_back(data2)" << endl;
map_data.insert(make_pair(, data2));
Sleep( * map_data.size()); cout << "****************map_data.push_back(data3)" << endl;
map_data.insert(make_pair(, data3));
Sleep( * map_data.size()); cout << "****************map_data.push_back(data4)" << endl;
map_data.insert(make_pair(, data4));
Sleep( * map_data.size()); cout << "****************map_data.push_back(data5)" << endl;
map_data.insert(make_pair(, data5));
Sleep( * map_data.size()); cout << "****************map_data.push_back(data6)" << endl;
map_data.insert(make_pair(, data6));
Sleep( * map_data.size()); cout << "****************map_data.push_back(data7)" << endl;
map_data.insert(make_pair(, data7));
Sleep( * map_data.size()); cout << "****************map_data.push_back(data8)" << endl;
map_data.insert(make_pair(, data8));
Sleep( * map_data.size()); cout << "****************map_data.push_back(data9)" << endl;
map_data.insert(make_pair(, data9));
Sleep( * map_data.size()); cout << "****************map_data.push_back(data10)" << endl;
map_data.insert(make_pair(, data10));
Sleep( * map_data.size()); // 程序到此为止,可以看到在每次push的时候,对象的复制构造函数会调用2次。为什么是2次?
cout << "===============show map by iterator" << endl;
for (map<int,CData>::iterator itr = map_data.begin(); itr != map_data.end(); itr++)
{
// 显示的地址信息,是每次push的时候所复制的对象的地址。
cout<<"key:"<< itr->first<<"\t" << "value:address of itr is " << &(itr->second) << " and msg is " << itr->second.toString() << endl;
} cout << "===============clear map 1" << endl;
// map中的元素不是指针,此处的清理,会调用析构函数。
map_data.clear(); cout << "****************map_data.push_back(data6)" << endl;
map_data.insert(make_pair(, data6));
Sleep( * map_data.size()); cout << "****************map_data.push_back(data2)" << endl;
map_data.insert(make_pair(, data2));
Sleep( * map_data.size()); cout << "****************map_data.push_back(data3)" << endl;
map_data.insert(make_pair(, data3));
Sleep( * map_data.size()); cout << "****************map_data.push_back(data1" << endl;
map_data.insert(make_pair(, data1));
Sleep( * map_data.size()); cout << "****************map_data.push_back(data10)" << endl;
map_data.insert(make_pair(, data10));
Sleep( * map_data.size()); cout << "****************map_data.push_back(data4)" << endl;
map_data.insert(make_pair(, data4));
Sleep( * map_data.size()); cout << "****************map_data.push_back(data7)" << endl;
map_data.insert(make_pair(, data7));
Sleep( * map_data.size()); cout << "****************map_data.push_back(data8)" << endl;
map_data.insert(make_pair(, data8));
Sleep( * map_data.size()); cout << "****************map_data.push_back(data9)" << endl;
map_data.insert(make_pair(, data9));
Sleep( * map_data.size()); cout << "****************map_data.push_back(data5)" << endl;
map_data.insert(make_pair(, data5));
Sleep( * map_data.size()); // 程序到此为止,可以看到在每次push的时候,对象的复制构造函数会调用2次。为什么是2次?
cout << "===============show map by iterator 2" << endl;
for (map<int, CData>::iterator itr = map_data.begin(); itr != map_data.end(); itr++)
{
// 显示的地址信息,是每次push的时候所复制的对象的地址。
// 可以看到,不管插入的数据是如何的乱序,map的结果是排好序的。
cout << "key:" << itr->first << "\t" << "value:address of itr is " << &(itr->second) << " and msg is " << itr->second.toString() << endl;
} cout << "===============clear map 2" << endl;
// map中的元素不是指针,此处的清理,会调用析构函数。
map_data.clear(); cout << "======================end of process" << endl; return ;
}
程序的输出结果:
process begin at 00C917FD
CData(int i,string &s) [sequence:1 | remark:baby_test] 0053F6F0
CData(int i,string &s) [sequence:2 | remark:baby_test] 0053F6C8
CData(int i,string &s) [sequence:3 | remark:baby_test] 0053F6A0
CData(int i,string &s) [sequence:4 | remark:baby_test] 0053F678
CData(int i,string &s) [sequence:5 | remark:baby_test] 0053F650
CData(int i,string &s) [sequence:6 | remark:baby_test] 0053F628
CData(int i,string &s) [sequence:7 | remark:baby_test] 0053F600
CData(int i,string &s) [sequence:8 | remark:baby_test] 0053F5D8
CData(int i,string &s) [sequence:9 | remark:baby_test] 0053F5B0
CData(int i,string &s) [sequence:10 | remark:baby_test] 0053F588
push CData to map
max size of list<CData> is 82595524
size of list<CData> is 0
****************map_data.push_back(data1)
CData(const CData &data) [sequence:1 | remark:baby_test] 0053F158
CData(const CData &data) [sequence:1 | remark:baby_test] 005F6F74
~CData() 0053F158
****************map_data.push_back(data2)
CData(const CData &data) [sequence:2 | remark:baby_test] 0053F108
CData(const CData &data) [sequence:2 | remark:baby_test] 005FE2A4
~CData() 0053F108
****************map_data.push_back(data3)
CData(const CData &data) [sequence:3 | remark:baby_test] 0053F0B8
CData(const CData &data) [sequence:3 | remark:baby_test] 005FE304
~CData() 0053F0B8
****************map_data.push_back(data4)
CData(const CData &data) [sequence:4 | remark:baby_test] 0053F068
CData(const CData &data) [sequence:4 | remark:baby_test] 005FE364
~CData() 0053F068
****************map_data.push_back(data5)
CData(const CData &data) [sequence:5 | remark:baby_test] 0053F018
CData(const CData &data) [sequence:5 | remark:baby_test] 005FE3C4
~CData() 0053F018
****************map_data.push_back(data6)
CData(const CData &data) [sequence:6 | remark:baby_test] 0053EFC8
CData(const CData &data) [sequence:6 | remark:baby_test] 005FE424
~CData() 0053EFC8
****************map_data.push_back(data7)
CData(const CData &data) [sequence:7 | remark:baby_test] 0053EF78
CData(const CData &data) [sequence:7 | remark:baby_test] 005FE484
~CData() 0053EF78
****************map_data.push_back(data8)
CData(const CData &data) [sequence:8 | remark:baby_test] 0053EF28
CData(const CData &data) [sequence:8 | remark:baby_test] 005FE4E4
~CData() 0053EF28
****************map_data.push_back(data9)
CData(const CData &data) [sequence:9 | remark:baby_test] 0053EED8
CData(const CData &data) [sequence:9 | remark:baby_test] 005FED9C
~CData() 0053EED8
****************map_data.push_back(data10)
CData(const CData &data) [sequence:10 | remark:baby_test] 0053EE88
CData(const CData &data) [sequence:10 | remark:baby_test] 005FEDFC
~CData() 0053EE88
===============show map by iterator
key:1 value:address of itr is 005F6F74 and msg is [sequence:1 | remark:baby_test]
key:2 value:address of itr is 005FE2A4 and msg is [sequence:2 | remark:baby_test]
key:3 value:address of itr is 005FE304 and msg is [sequence:3 | remark:baby_test]
key:4 value:address of itr is 005FE364 and msg is [sequence:4 | remark:baby_test]
key:5 value:address of itr is 005FE3C4 and msg is [sequence:5 | remark:baby_test]
key:6 value:address of itr is 005FE424 and msg is [sequence:6 | remark:baby_test]
key:7 value:address of itr is 005FE484 and msg is [sequence:7 | remark:baby_test]
key:8 value:address of itr is 005FE4E4 and msg is [sequence:8 | remark:baby_test]
key:9 value:address of itr is 005FED9C and msg is [sequence:9 | remark:baby_test]
key:10 value:address of itr is 005FEDFC and msg is [sequence:10 | remark:baby_test]
===============clear map 1
~CData() 005FEDFC
~CData() 005FED9C
~CData() 005FE4E4
~CData() 005FE484
~CData() 005FE424
~CData() 005FE3C4
~CData() 005FE364
~CData() 005FE304
~CData() 005FE2A4
~CData() 005F6F74
****************map_data.push_back(data6)
CData(const CData &data) [sequence:6 | remark:baby_test] 0053EDE0
CData(const CData &data) [sequence:6 | remark:baby_test] 005F6F74
~CData() 0053EDE0
****************map_data.push_back(data2)
CData(const CData &data) [sequence:2 | remark:baby_test] 0053ED90
CData(const CData &data) [sequence:2 | remark:baby_test] 005FED9C
~CData() 0053ED90
****************map_data.push_back(data3)
CData(const CData &data) [sequence:3 | remark:baby_test] 0053ED40
CData(const CData &data) [sequence:3 | remark:baby_test] 005FEDFC
~CData() 0053ED40
****************map_data.push_back(data1
CData(const CData &data) [sequence:1 | remark:baby_test] 0053ECF0
CData(const CData &data) [sequence:1 | remark:baby_test] 005FEE5C
~CData() 0053ECF0
****************map_data.push_back(data10)
CData(const CData &data) [sequence:10 | remark:baby_test] 0053ECA0
CData(const CData &data) [sequence:10 | remark:baby_test] 005FEEBC
~CData() 0053ECA0
****************map_data.push_back(data4)
CData(const CData &data) [sequence:4 | remark:baby_test] 0053EC50
CData(const CData &data) [sequence:4 | remark:baby_test] 005FEF1C
~CData() 0053EC50
****************map_data.push_back(data7)
CData(const CData &data) [sequence:7 | remark:baby_test] 0053EC00
CData(const CData &data) [sequence:7 | remark:baby_test] 005FEF7C
~CData() 0053EC00
****************map_data.push_back(data8)
CData(const CData &data) [sequence:8 | remark:baby_test] 0053EBB0
CData(const CData &data) [sequence:8 | remark:baby_test] 005FE2A4
~CData() 0053EBB0
****************map_data.push_back(data9)
CData(const CData &data) [sequence:9 | remark:baby_test] 0053EB60
CData(const CData &data) [sequence:9 | remark:baby_test] 005FE304
~CData() 0053EB60
****************map_data.push_back(data5)
CData(const CData &data) [sequence:5 | remark:baby_test] 0053EB10
CData(const CData &data) [sequence:5 | remark:baby_test] 005FE364
~CData() 0053EB10
===============show map by iterator 2
key:1 value:address of itr is 005FEE5C and msg is [sequence:1 | remark:baby_test]
key:2 value:address of itr is 005FED9C and msg is [sequence:2 | remark:baby_test]
key:3 value:address of itr is 005FEDFC and msg is [sequence:3 | remark:baby_test]
key:4 value:address of itr is 005FEF1C and msg is [sequence:4 | remark:baby_test]
key:5 value:address of itr is 005FE364 and msg is [sequence:5 | remark:baby_test]
key:6 value:address of itr is 005F6F74 and msg is [sequence:6 | remark:baby_test]
key:7 value:address of itr is 005FEF7C and msg is [sequence:7 | remark:baby_test]
key:8 value:address of itr is 005FE2A4 and msg is [sequence:8 | remark:baby_test]
key:9 value:address of itr is 005FE304 and msg is [sequence:9 | remark:baby_test]
key:10 value:address of itr is 005FEEBC and msg is [sequence:10 | remark:baby_test]
===============clear map 2
~CData() 005FEEBC
~CData() 005FE304
~CData() 005FE2A4
~CData() 005FEF7C
~CData() 005F6F74
~CData() 005FE364
~CData() 005FEF1C
~CData() 005FEDFC
~CData() 005FED9C
~CData() 005FEE5C
======================end of process
~CData() 0053F588
~CData() 0053F5B0
~CData() 0053F5D8
~CData() 0053F600
~CData() 0053F628
~CData() 0053F650
~CData() 0053F678
~CData() 0053F6A0
~CData() 0053F6C8
~CData() 0053F6F0
map的内存分配机制分析的更多相关文章
- list的内存分配机制分析
该程序演示了list在内存分配时候的问题.里面的备注信息是我的想法. /* 功能说明: list的内存分配机制分析. 代码说明: list所管理的内存地址可以是不连续的.程序在不断的push_back ...
- vector的内存分配机制分析
该程序初步演示了我对vector在分配内存的时候的理解.可能有误差,随着理解的改变,改代码可以被修改. /* 功能说明: vector的内存分配机制分析. 代码说明: vector所管理的内存地址是连 ...
- memcached学习——memcached的内存分配机制Slab Allocation、内存使用机制LRU、常用监控记录(四)
内存分配机制Slab Allocation 本文参考博客:https://my.oschina.net/bieber/blog/505458 Memcached的内存分配是以slabs为单位的,会根据 ...
- Keil C动态内存管理机制分析及改进(转)
源:Keil C动态内存管理机制分析及改进 Keil C是常用的嵌入式系统编程工具,它通过init_mempool.mallloe.free等函数,提供了动态存储管理等功能.本文通过对init_mem ...
- Go语言内存分配机制
前言: 本文是学习<<go语言程序设计>> -- 清华大学出版社(王鹏 编著) 的2014年1月第一版 做的一些笔记 , 如有侵权, 请告知笔者, 将在24小时内删除, 转载请 ...
- 阿里面试官:小伙子,你给我说一下JVM对象创建与内存分配机制吧
内存分配机制 逐步分析 类加载检查: 虚拟机遇到一条new指令(new关键字.对象的克隆.对象的序列化等)时,会先去检查这个指令的参数在常量池中定位到一个类的符号引用,并且这个符号引用代表的类是否 ...
- JVM的艺术-对象创建与内存分配机制深度剖析
JVM的艺术-对象创建与内存分配机制深度剖析 引言 本章将介绍jvm的对象创建与内存分配.彻底带你了解jvm的创建过程以及内存分配的原理和区域,以及包含的内容. 对象的创建 类加载的过程 固定的类加载 ...
- Memcache简介 & 内存分配机制
关于这个东西里面到底应该存放数据网上一直有很多种说法,有的说sql进行md5之后作为键值,结果作为内容存放,也有人说按照业务逻辑错放,反正是炒的不亦乐乎. 本人经过将近2 ...
- SQL Server ->> Memory Allocation Mechanism and Performance Analysis(内存分配机制与性能分析)之 -- Minimum server memory与Maximum server memory
Minimum server memory与Maximum server memory是SQL Server下配置实例级别最大和最小可用内存(注意不等于物理内存)的服务器配置选项.它们是管理SQL S ...
随机推荐
- CuteEditor.Editor+a+a+c+a+a.a() System.RuntimeType.get_Assembly() 问题解决方法
问题: Server Error in '/' Application. Attempt by method 'CuteEditor.Editor+a+a+c+a+a.a()' to access m ...
- SpringMVC:学习笔记(9)——文件下载
SpringMVC—文件下载 说明 两个案例 1.为登录用户提供下载服务. 2.阻止仅通过输入网址即可获取下载. 文件下载概览 为了将文件发送给浏览器,我们需要在控制器中完成以下操作: 对请求处理方法 ...
- 工作了3年的JAVA程序员应该具备什么技能?(转)
工作了3年的JAVA程序员应该具备什么技能? 因为和同事有约定再加上LZ自己也喜欢做完一件事之后进行总结,因此有了这篇文章.这篇文章大部分内容都是面向整个程序员群体的,当然因为LZ本身是做Java开发 ...
- Loadrunder之脚本篇——参数化在场景中的运用
Action() { lr_eval_string("{NewParam}"); lr_eval_string("{NewParam}"); return 0; ...
- 动手动脑:String.equals()的使用方法
public class StringEquals { /** * @param args the command line arguments */ public static void main( ...
- HTML学习笔记(上)
1. HTML介绍 1.1 什么是HTML HyperText Markup Language,超文本标记语言.简单来说,HTML文件本质上就是一个文本文件,但是这个文本文件是带有标签的. 不同的标签 ...
- JQuery3 的新变化
1. for-of 循环 for-in 循环不被推荐遍历数组,forEach 循环不能中断,for-of 循环(ES6)则弥补了前两者的不足,又添加了更多拓展(比如能遍历字符串,DOM 元素等) 因此 ...
- IMX6Q RTC驱动分析
对于在工作中学习驱动的,讲究的是先使用,再理解.好吧,我们来看看板子里是如何注册的? 在板文件里,它的注册函数是这样的: imx6q_add_imx_snvs_rtc() 好吧,让我们追踪下去: 1 ...
- 内核模块编译时怎样绕过insmod时的版本检查
1.Uboot:每个arm芯片或者海斯芯片都有各自的uboot. 2.但他们的内核版本可以是一样的,主要是跟各自内核的进行的编译选项有关, 31的内核版本里加了版本检查选项“Kernel type-& ...
- 【Head First Servlets and JSP】迷你MVC:JarDownload的完整实现
1.首先,写一个download.html放至D:\apache-tomcat-7.0.77\webapps\JarDownload-v1. <!DOCTYPE HTML> <htm ...