码迷,mamicode.com
首页 > 编程语言 > 详细

走进C++程序世界-----operator new delete 重载

时间:2014-04-28 10:43:41      阅读:406      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   使用   os   io   

 在C++ 的世界里,new 和delete 是关键字,而在C的世界里相对应的malloc和free是函数,关键C++的new和delete分析,详见前面的章节,这里就不在过多的介绍了。链接

下面来研究下关于new 和delete的重载。

 1、对比使用重载和未使用重载

 未使用“
/*File : operator_new.cpp
 *Auth : sjin
 *Date : 2014-04-27
 *Mail : 413977243@qq.com
 */

#include <iostream>

using namespace std;

class test {
public:
	test(){cout << "*****构造test()*****"<< endl;};
	~test(){cout << "+++++析构test()+++++"<< endl;};
};

int main()
{
	test * x = new test;//执行分配空间,再执行析构函数
	delete x;//先执行析构函数,在释放空间
}

使用重载后,

/*File : operator_new.cpp
 *Auth : sjin
 *Date : 2014-04-27
 *Mail : 413977243@qq.com
 */

#include <iostream>

using namespace std;

char mem[10000] = {‘\0‘};
int pos = 0;

class test {
public:
	test(){cout << "*****构造test()*****"<< endl;};
	~test(){cout << "+++++析构test()+++++"<< endl;};

public:
	void * operator new(size_t bytes){
		cout << "------new test()------" << endl;
		int alloc = pos;
		pos += bytes;
		return (mem + alloc);
	};

	void operator delete(void *){
		cout << "------delete test()------" << endl;

	};
};

int main()
{
	test * x = new test;

	delete x;
}

mamicode.com,码迷


/*File : operator_new.cpp
 *Auth : sjin
 *Date : 2014-04-27
 *Mail : 413977243@qq.com
 */

#include <iostream>

using namespace std;

char mem[10000] = {‘\0‘};
int pos = 0;

class test {
public:
	test(){cout << "*****构造test()*****"<< endl;};
	~test(){cout << "+++++析构test()+++++"<< endl;};

public:
	void * operator new(size_t bytes){
		cout << "------new test()------" << endl;
		int alloc = pos;
		pos += bytes;
		return (mem + alloc);
	};

	void operator delete(void *){
		cout << "------delete test()------" << endl;

	};
};

int main()
{
	test * x = new test;

	delete x;

	x = new test[3];

	delete [] x;//这里对数组释放,需要注意
}

mamicode.com,码迷


#include <iostream>

using namespace std;

char mem[10000] = {‘\0‘};
int pos = 0;

class test {
public:
	test(){cout << "*****构造test()*****"<< endl;};
	~test(){cout << "+++++析构test()+++++"<< endl;};

public:
	void * operator new(size_t bytes){
		cout << "------new test()------" << endl;
		int alloc = pos;
		pos += bytes;
		return (mem + alloc);
	};

	void operator delete(void *){
		cout << "------delete test()------" << endl;

	};

	void * operator new[](size_t bytes){
		cout << "------new test()------" << endl;
		int alloc = pos;
		pos += bytes;
		return (mem + alloc);
	};

	void operator delete[](void *){
		cout << "------delete test()------" << endl;

	};
};

int main()
{
	test * x = new test;

	delete x;

	x = new test[3];

	delete [] x;
}

mamicode.com,码迷

对new 和delete 函数的重载可以用来检测 内存泄露的情况。



 










 


 


走进C++程序世界-----operator new delete 重载,码迷,mamicode.com

走进C++程序世界-----operator new delete 重载

标签:style   blog   http   使用   os   io   

原文地址:http://blog.csdn.net/sjin_1314/article/details/24603719

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!