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。
按任意键关闭此窗口. . .
可以看到,使用buffer
的new
会使得地址覆盖,也没有自动调用析构函数。下面是修改之后的代码:
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。
按任意键关闭此窗口. . .