用途:
我们都知道内存申请是耗资源的,而且持续的new,free容易产生内存碎片,所以一般在性能要求极高的场合,比如在服务器编程,我们会事先在堆空间分配好内存池,然后使用placement new 复用内存池的内存。
栗子:
#include<new>
#include<iostream>
using namespace std;
int main()
{
int *p=new int[20];
int *p1=new(p) int[5];
int *p2=new(p1+5) int[5];
int *p3=new(p2+5) int[5];
int *p4=new(p3+5) int[5];
cout<<"The addr of p is :"<<p<<endl;
cout<<"The addr of p1 is :"<<p1<<endl;
cout<<"The addr of p2 is :"<<p2<<endl;
cout<<"The addr of p3 is :"<<p3<<endl;
cout<<"The addr of p4 is :"<<p4<<endl;
delete []p;
}