布局new分配 ,

布局new和普通New不同,相当于建立一个小堆空间 。

#include <iostream>
#include <string>
#include <new>
using namespace std;
const int BUF = 512;
class testing
{
private :
string word;
int len;
public:
testing(const string & s = "tesing",int n = 0 )
{
word = s;
len = n;
cout <<word <<"construted"<<endl;
}
~testing()
{
cout << word <<"destroy" <<endl;
}
void show() const
{
cout << word <<"," <<len <<endl;
}
};

int main()
{
char *buffer = new char[BUF];
testing *pc1,*pc2;
pc1 = new( buffer) testing;
pc2 = new testing("heap1" , 20);

cout <<"memory block address :" <<(void *)buffer << "heap1" << pc2<<endl;

cout <<"memory content" << pc1 <<":";
pc1->show();
cout << pc2 <<":";
pc2->show();

testing *p3 , *p4;
p3 = new (buffer + sizeof(testing)) testing("third",3);
delete pc2;
pc1->~testing();
p3->~testing();
delete [] buffer;
return 0;
}
posted on 2012-03-23 22:15 lzhenf 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/lzhenf/archive/2012/03/23/2414352.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值