map的内存分配机制分析

该程序演示了map在形成的时候对内存的操作和分配。

因为自己对平衡二叉树的创建细节理解不够,还不太明白程序所显示的日志。等我明白了,再来修改这个文档。


/*
功能说明:
	map的内存分配机制分析。
代码说明:
	map所管理的内存地址可以是不连续的。如果key是可以通过<排序的,那么,map最后的结果是有序的。它是通过一个平衡二叉树来保存数据。所以,其查找效率极高。
实现方式:

限制条件或者存在的问题:
	无
*/
#include 
#include 
#include 

#include 

using namespace std;

class CData
{
public:
	CData()
	{
		sequence = 0;
		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[2048] = { 0 };
		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(1, str);
	CData data2(2, str);
	CData data3(3, str);
	CData data4(4, str);
	CData data5(5, str);
	CData data6(6, str);
	CData data7(7, str);
	CData data8(8, str);
	CData data9(9, str);
	CData data10(10, str);

	cout << "push CData to map" << endl;
	map map_data;
	cout << "max size of list is " << map_data.max_size() << endl;
	cout << "size of list is " << map_data.size() << endl;

	cout << "****************map_data.push_back(data1)" << endl;
	map_data.insert(make_pair(1, data1));
	Sleep(2 * map_data.size());
	
	cout << "****************map_data.push_back(data2)" << endl;
	map_data.insert(make_pair(2, data2));
	Sleep(2 * map_data.size());

	cout << "****************map_data.push_back(data3)" << endl;
	map_data.insert(make_pair(3, data3));
	Sleep(2 * map_data.size());

	cout << "****************map_data.push_back(data4)" << endl;
	map_data.insert(make_pair(4, data4));
	Sleep(2 * map_data.size());

	cout << "****************map_data.push_back(data5)" << endl;
	map_data.insert(make_pair(5, data5));
	Sleep(2 * map_data.size());

	cout << "****************map_data.push_back(data6)" << endl;
	map_data.insert(make_pair(6, data6));
	Sleep(2 * map_data.size());

	cout << "****************map_data.push_back(data7)" << endl;
	map_data.insert(make_pair(7, data7));
	Sleep(2 * map_data.size());

	cout << "****************map_data.push_back(data8)" << endl;
	map_data.insert(make_pair(8, data8));
	Sleep(2 * map_data.size());

	cout << "****************map_data.push_back(data9)" << endl;
	map_data.insert(make_pair(9, data9));
	Sleep(2 * map_data.size());

	cout << "****************map_data.push_back(data10)" << endl;
	map_data.insert(make_pair(10, data10));
	Sleep(2 * map_data.size());

	// 程序到此为止,可以看到在每次push的时候,对象的复制构造函数会调用2次。为什么是2次?
	cout << "===============show map by iterator" << endl;
	for (map::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(6, data6));
	Sleep(2 * map_data.size());

	cout << "****************map_data.push_back(data2)" << endl;
	map_data.insert(make_pair(2, data2));
	Sleep(2 * map_data.size());

	cout << "****************map_data.push_back(data3)" << endl;
	map_data.insert(make_pair(3, data3));
	Sleep(2 * map_data.size());

	cout << "****************map_data.push_back(data1" << endl;
	map_data.insert(make_pair(1, data1));
	Sleep(2 * map_data.size());

	cout << "****************map_data.push_back(data10)" << endl;
	map_data.insert(make_pair(10, data10));
	Sleep(2 * map_data.size());

	cout << "****************map_data.push_back(data4)" << endl;
	map_data.insert(make_pair(4, data4));
	Sleep(2 * map_data.size());

	cout << "****************map_data.push_back(data7)" << endl;
	map_data.insert(make_pair(7, data7));
	Sleep(2 * map_data.size());

	cout << "****************map_data.push_back(data8)" << endl;
	map_data.insert(make_pair(8, data8));
	Sleep(2 * map_data.size());

	cout << "****************map_data.push_back(data9)" << endl;
	map_data.insert(make_pair(9, data9));
	Sleep(2 * map_data.size());

	cout << "****************map_data.push_back(data5)" << endl;
	map_data.insert(make_pair(5, data5));
	Sleep(2 * map_data.size());

	// 程序到此为止,可以看到在每次push的时候,对象的复制构造函数会调用2次。为什么是2次?
	cout << "===============show map by iterator 2" << endl;
	for (map::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 0;
}

程序的输出结果:
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 is 82595524
size of list 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

你可能感兴趣的:(小程序)