c++比较buffer上的new和常规的new

main.cpp

#include<iostream>
#include<string>

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]; // get a block of memory
	JustTesting* pc1, * pc2;

	pc1 = new (buffer) JustTesting; // place object in buffer
	pc2 = new JustTesting("heap1", 20); // place object on heap

	cout << "memory block address:\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; // free heap1
	delete pc4; // free heap2
	delete[] buffer; // free buffer

	cout << "done\n";

	return 0;
}

结果

Just Testing constructed
heap1 constructed
memory block address:
buffer: 00000203D2AB3C00 heap: 00000203D2AAD910
memory contents:
00000203D2AB3C00: Just Testing, 0
00000203D2AAD910: heap1, 20
Bad Idea constructed
heap2 constructed
Memory contents:
00000203D2AB3C00: Bad Idea, 6
00000203D2AB63A0: heap2, 10
heap1 destroyed
heap2 destroyed
done

E:\projectFiles\CPPcodes\learn_cpp\DynamicMemory003\x64\Release\DynamicMemory003.exe (进程 17840)已退出,代码为 0。
按任意键关闭此窗口. . .

可以看到,使用buffernew会使得地址覆盖,也没有自动调用析构函数。下面是修改之后的代码:

main2.cpp

#include<iostream>
#include<string>

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; // place object in buffer
	pc2 = new JustTesting("Heap1", 20); // place object on heap

	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;
	JustTesting* pc4;
	// fix placement new location
	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;
	// explicitly destroy placement new objects
	pc3->~JustTesting();
	pc1->~JustTesting();	
	delete[] buffer;
	
	cout << "done\n";
	return 0;
}

结果

Just Testing constructed
Heap1 constructed
Memory block addresses:
buffer: 0000021207E83C60 heap: 0000021207E7F410
Memory contents:
0000021207E83C60: Just Testing, 0
0000021207E7F410: Heap1, 20
better idea constructed
Heap2 constructed
Memory contents:
0000021207E83C88: better idea, 6
0000021207E7F230: Heap2, 10
Heap1 destroyed
Heap2 destroyed
better idea destroyed
Just Testing destroyed
done

E:\projectFiles\CPPcodes\learn_cpp\DynamicMemory003\x64\Release\DynamicMemory003.exe (进程 22852)已退出,代码为 0。
按任意键关闭此窗口. . .
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值