placement new机制

用途:

     我们都知道内存申请是耗资源的,而且持续的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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值