元素为指针的vector的使用说明

该程序演示了vector中的元素为指针的时候的对对象的操作。

/*
功能说明:
	元素为指针的vector的使用说明
实现方式:
	使用this成员来显示各个对象的地址。
限制条件或者存在的问题:
	无
*/
#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;
	}

	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";
	// 查看保存在系统的栈中的对象的地址。
	cout << "==========test class object in stack" << endl;
	CData data1;
	cout << "address of object in stack:" << &data1 << "\tdefalut object elements:" << data1.toString() << endl;
	data1.setSequence(11);
	data1.setRemark(str);
	cout << "address of object in stack:" << &data1 << "\tafter set object elements:" << data1.toString() << endl;

	// 查看保存在系统的堆中的对象的地址。这种对象需要手工删除,否则会造成内存泄露
	cout << "\n==========test class object in heap" << endl;
	CData *p_data12 = new CData(12, str); // 带有参数的构造函数的第二个参数为一个地址,所以,此处的调用必须为一个对象。不能是一个字符数组。
	cout << "address of pointer of class object in stack:" << &p_data12 << "\taddress of class object in heap:" << p_data12 << "\tobject in heap:" << p_data12->toString() << endl;

	CData *p_data13 = new CData(13, str);
	cout << "address of pointer of class object in stack:" << &p_data13 << "\taddress of class object in heap:" << p_data13 << "\tobject in heap:" << p_data13->toString() << endl;

	cout << "\n==========vector test. object in heap" << endl;
	vector vec_data;
	vec_data.push_back(p_data12);
	vec_data.push_back(p_data13);
	vec_data.push_back(p_data12);
	vec_data.push_back(p_data13);
	// 上述操作完成之后,堆中有2个对象,但是,这两个对象分别有3个指针来指向他。一个是原始的创建对象的时候的。另外两个是上面的push_back所复制的。
	cout << "show vector's msg" << endl;
	for (int i = 0; i < vec_data.size(); i++)
	{
		// 查看vector元素的地址,说明,vector中的元素的位置是连续的。
		cout << "address of element in vector:" << &(vec_data[i]) << endl;
		CData *pTmp = vec_data[i];
		cout << "object address in heap:" << pTmp << "\tobject msg in heap:" << (pTmp)->toString() << endl;
	}

	vec_data.clear();
	// 清理掉vector中的元素。也仅是清理掉vector中的指向对象的指针,并没有清理掉这些指针所指向的对象。另外,其原始的指向对象的指针还是存在的。
	cout << "\n==========check objects in heap after clear vector" << endl;
	cout << "address of class object in heap:" << p_data12 << "\tobject in heap:" << p_data12->toString() << endl;
	cout << "address of class object in heap:" << p_data13 << "\tobject in heap:" << p_data13->toString() << endl;

	cout << "\n==========delete object in heap using vector iterator" << endl;
	// 再将两个对象的指针复制到vector中。
	vec_data.push_back(p_data12);
	vec_data.push_back(p_data13);
	for (vector::iterator itr = vec_data.begin(); itr != vec_data.end(); itr++)
	{
		// 通过迭代器来删除每个指针指向的对象。如果vector中的多个元素指向了同一个对象(上面的情形),此处的程序会异常退出。
		cout <<"delete object:" << *itr << endl;
		delete *itr;
		*itr = 0;
	}

	// 清理vector元素中的指针所指向的对象。并不意味着去清理了vector本身,所以,其长度还是原来的数值。
	cout << "vector length is " << vec_data.size() << endl;
	cout << "\n==========check objects in heap using vector after delete objects" << endl;
	for (vector::iterator itr = vec_data.begin(); itr != vec_data.end(); itr++)
	{
		// vecotr中的指针已经在上面被修改,这里是查看一下。
		cout << "address of object in heap:" << *itr << endl;
	}

	vec_data.clear();

	cout << "\n==========check objects in heap using original pointer after delete objects" << endl;
	// 上面的vector中,已经删除的堆中的对象。所以下面的对象的toString()操作将是错误的。使用此函数,会导致程序异常退出。
	//cout << "address of class object in heap:" << p_data12 << "\tobject in heap:" << p_data12->toString() << endl;
	//cout << "address of class object in heap:" << p_data13 << "\tobject in heap:" << p_data13->toString() << endl;
	
	// 上面的vector中,已经删除了堆中的对象。但是原始的指向对象的指针还是存在的,只不过是其指向的内存已经无效。
	cout << "address of class object in heap:" << p_data12 << endl;
	cout << "address of class object in heap:" << p_data13 << endl;

	// 此处相当于去删除一个无效的堆的对象,会导致程序异常退出。
	//delete p_data12;
	//delete p_data13;

	// 其所指向的对象已经在vector中删除,所以需要将其指向的地址清理掉,防止错误。
	p_data12 = 0;
	p_data13 = 0;

	cout << "process end " << (void*)&main << endl;
	
	// 程序退出前,会自动清理掉栈中的对象。

	return 0;
}

程序的输出结果:

process begin at 002D170D
==========test class object in stack
CData() [sequence:0 | remark:default string]    008FF9D8
address of object in stack:008FF9D8     defalut object elements:[sequence:0 | remark:default string]
address of object in stack:008FF9D8     after set object elements:[sequence:11 | remark:baby test]

==========test class object in heap
CData(int i,string &s)  [sequence:12 | remark:baby test]        00906BB8
address of pointer of class object in stack:008FF9CC    address of class object in heap:00906BB8        object in heap:[sequence:12 | remark:baby test]
CData(int i,string &s)  [sequence:13 | remark:baby test]        00902F30
address of pointer of class object in stack:008FF9C0    address of class object in heap:00902F30        object in heap:[sequence:13 | remark:baby test]

==========vector test. object in heap
show vector's msg
address of element in vector:0090B8E8
object address in heap:00906BB8 object msg in heap:[sequence:12 | remark:baby test]
address of element in vector:0090B8EC
object address in heap:00902F30 object msg in heap:[sequence:13 | remark:baby test]
address of element in vector:0090B8F0
object address in heap:00906BB8 object msg in heap:[sequence:12 | remark:baby test]
address of element in vector:0090B8F4
object address in heap:00902F30 object msg in heap:[sequence:13 | remark:baby test]

==========check objects in heap after clear vector
address of class object in heap:00906BB8        object in heap:[sequence:12 | remark:baby test]
address of class object in heap:00902F30        object in heap:[sequence:13 | remark:baby test]

==========delete object in heap using vector iterator
delete object:00906BB8
~CData()        00906BB8
delete object:00902F30
~CData()        00902F30
vector length is 2

==========check objects in heap using vector after delete objects
address of object in heap:00000000
address of object in heap:00000000

==========check objects in heap using original pointer after delete objects
address of class object in heap:00906BB8
address of class object in heap:00902F30
process end 002D170D
~CData()        008FF9D8

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