[C++] 纯文本查看 复制代码
// newplace.cpp -- using placement new
#include <iostream>
#include <new> // 使用定位 new 所需要的头文件
const int BUF = 512;
const int N = 5;
char buffer[BUF]; // 数组缓冲区
int main()
{
using namespace std;
double *pd1, *pd2;
int i;
cout << "Calling new and placement new:\n";
pd1 = new double[N]; // 从堆中分配
pd2 = new (buffer) double[N]; // 使用缓冲数组
for (i = 0; i < N; i++)
pd2 = pd1 = 1000 + 20.0 * i;
cout << "Memory addresses:\n" << " heap: " << pd1
<< " static: " << (void *) buffer <<endl;
cout << "Memory contents:\n";
for (i = 0; i < N; i++)
{
cout << pd1 << " at " << &pd1 << "; ";
cout << pd2 << " at " << &pd2 << endl;
}
cout << "\nCalling new and placement new a second time:\n";
double *pd3, *pd4;
pd3= new double[N]; // 从堆中分配一块新的内存
pd4 = new (buffer) double[N]; // 覆盖了原来的数据
for (i = 0; i < N; i++)
pd4 = pd3 = 1000 + 40.0 * i;
cout << "Memory contents:\n";
for (i = 0; i < N; i++)
{
cout << pd3 << " at " << &pd3 << "; ";
cout << pd4 << " at " << &pd4 << endl;
}
cout << "\nCalling new and placement new a third time:\n";
delete [] pd1;
pd1= new double[N];
pd2 = new (buffer + N * sizeof(double)) double[N];
for (i = 0; i < N; i++)
pd2 = pd1 = 1000 + 60.0 * i;
cout << "Memory contents:\n";
for (i = 0; i < N; i++)
{
cout << pd1 << " at " << &pd1 << "; ";
cout << pd2 << " at " << &pd2 << endl;
}
delete [] pd1;
delete [] pd3;
// cin.get();
return 0;
}
[C++] 纯文本查看 复制代码
#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; // 将对象放入 buffer[] 中
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();
delete pc1;
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; // 释放 Heap1
delete pc4; // 释放 Heap2
delete [] buffer; // 释放 buffer
cout << "Done\n";
// std::cin.get();
return 0;
}
[C++] 纯文本查看 复制代码
#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; // 将对象放入 buffer[] 中
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; // 释放 Heap1
delete pc4; // 释放 Heap2
// 显示销毁由定位 new 创建的对象,注意调用的顺序
pc3->~JustTesting(); // 销毁由 pc3 指向的对象
pc1->~JustTesting(); // 销毁由 pc1 指向的对象
delete [] buffer; // 释放 buffer
return 0;
}