c++常规new和定位(placement)new

定位放置new操作的语法形式不同于普通的new操作。例如,一般都用如下语句A* p=new A;申请空间,而定位放置new操作则使用如下语句A* p=new (ptr) A;申请空间,其中ptr就是程序员指定的内存首地址。

通常,定位new负责在堆中找到一个足以能满足要求的内存块。new运算符还有另外一种变体,被称为定位(placement)new运算符,他让您能够制定要使用的位置。

例子如下:

#include<iostream>
#include<string>
#include<new>

using namespace std;
const int BUF = 512;

class JustTesting
{
private:
	string words;
	int number;

public:
	JustTesting(const string &s = "Just Testing", int n = 0)
	{
		words = s;
		number = n;
		cout << words << " constructed\n";
	}
	~JustTesting()
	{
		cout << words << " destroyed\n";
	}

	void Show()const
	{
		cout << words << ", " << number << endl;
	}
};

int main()
{
	char * buffer = new char[BUF];

	JustTesting *pc1, *pc2;

	pc1 = new (buffer) JustTesting;
	pc2 = new JustTesting("heap1", 20);

	cout << "Memory block addresses:\n" << "buffer: " << (void *)buffer << "  heap: " << pc2 << endl;
	cout << "Memory contents:\n";
	cout << pc1 << ": ";
	pc1->Show();
	cout << pc2 << ": ";
	pc2->Show();

	JustTesting *pc3, *pc4;
	pc3 = new(buffer) JustTesting("Bad Idea", 6);
	pc4 = new JustTesting("Heap2", 10);

	cout << "Memory contents:\n";
	cout << pc3 << ": ";
	pc3->Show();
	cout << pc4 << ": ";
	pc4->Show();

	delete pc2;
	delete pc4;
	delete []buffer;

	cout << "Done\n";

	return 0;
}
这个程序出现两个问题。第一:在创建第二个对象时,定位new运算符使用第一个新对象来覆盖用于第一个对象的内存单元。显然,如果类动态地为其成员分配内存,将引发问题。

第二:将delete用于pc2和pc4时,将自动调用为pc2和pc4指向的对象调用析构函数;然而,将delete[]用于buffer时,不会使用定位new运算符创建的对象调用析构函数。

不能使用delete pc1;delete pc3。原因在于delete可以和常规new运算符配合使用,但是不能和定位new运算符配合使用。

代码二:

#include<iostream>
#include<string>
#include<new>
using namespace std;
const int BUF = 512;

class JustTesting {
private:
	string words;
	int number;

public:
	JustTesting(const string &s = "Just Testing", int n = 0)
	{
		words = s;
		number = n;
		cout << words << " constructed\n";
	}

	~JustTesting() 
	{
		cout << words << " destroyed\n";
	}

	void Show()const
	{
		cout << words << ", " << number << endl;
	}
};

int main()
{
	char * buffer = new char[BUF];

	JustTesting *pc1, *pc2;
	pc1 = new (buffer)JustTesting;
	pc2 = new JustTesting("Heap1", 20);
	
	cout << "Memory block addresses:\n" << " buffer: " << (void*)buffer << " heap: " << pc2 << endl;
	cout << "Memory contents:\n";
	cout << pc1 << ": ";
	pc1->Show();
	cout << pc2 << ": ";
	pc2->Show();

	JustTesting *pc3, *pc4;
	pc3 = new(buffer + sizeof(JustTesting)) JustTesting("Better Idea", 6);
	pc4 = new JustTesting("Heap2", 10);

	cout << "Memory contents:\n";
	cout << pc3 << ":";
	pc3->Show();
	cout << pc4 << ": ";
	pc4->Show();

	delete pc2;
	delete pc4;

	pc3->~JustTesting();
	pc1->~JustTesting();

	delete[]buffer;
	cout << "Done\n";

	while (1);
	return 0;
}
解决这种问题方案是,显式地调用析构函数,必须指定要销毁的对象。需要注意一点是,正确的删除顺序。应以与创建顺序相反的顺序进行删除。原因在于,晚创建的对象可以依赖于早创建的对象。对象创建完成过后,才能释放用于存储这些对象的缓冲区。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值