您的位置:首页 > 编程语言 > C语言/C++

关于c++ new的三种使用方式

2017-01-08 00:00 411 查看
(1)new有三种使用方式:plain new、nothrow new、placement new.

1.plain new/delete.普通的new,定义如下:

void *operator new(std::size_t) throw(std::bad_alloc);

void operator delete(void*) throw();

注:标准C++ plain new失败后抛出标准异常std::bad_alloc而非返回NULL,因此检查返回值是否为NULL判断分配是否成功是徒劳的。

测试程序:

#include "stdafx.h"
#include <iostream>
using namespace std;

char *GetMemory(unsigned long size)
{
char *p=new char[size];//分配失败,不是返回NULL
return p;
}

int main()
{
try
{
char *p=GetMemory(10e11);// 分配失败抛出异常std::bad_alloc
//...........
if(!p)//徒劳
cout<<"failure"<<endl;
delete [] p;

}
catch(const std::bad_alloc &ex)
{
cout<<ex.what()<<endl;
}

return 0;
}

2.nothrow new/delete不抛出异常的运算符new的形式,new失败时返回NULL。

定义如下:

void *operator new(std::size_t,const std::nothrow_t&) throw();

void operator delete(void*) throw();

struct nothrow_t{}; const nothrow_t nothrow;//nothrow作为new的标志性哑元

测试程序:

#include "stdafx.h"
#include <iostream>
#include <new>
using namespace std;

char *GetMemory(unsigned long size)
{
char *p=new(nothrow) char[size];//分配失败,是返回NULL
if(NULL==p)
cout<<"alloc failure!"<<endl;
return p;
}

int main()
{
try
{
char *p=GetMemory(10e11);
//...........
if(p==NULL)
cout<<"failure"<<endl;
delete [] p;

}
catch(const std::bad_alloc &ex)
{
cout<<ex.what()<<endl;
}

return 0;
}

3.placement new/delete 主要用途是:反复使用一块较大的动态分配成功的内存来构造不同类型的对象或者它们的数组。例如可以先申请一个足够大的字符数组,然后当需要时在它上面构造不同类型的对象或数组。placement new不用担心内存分配失败,因为它根本不分配内存,它只是调用对象的构造函数。

测试程序:

#include "stdafx.h"
#include <iostream>
#include <new>
using namespace std;

class ADT
{
int i;
int j;
public:
ADT()
{
}
~ADT()
{
}
};

int main()
{
char *p=new(nothrow) char[sizeof(ADT)+2];
if(p==NULL)
cout<<"failure"<<endl;

ADT *q=new(p) ADT; //placement new:不必担心失败
// delete q;//错误!不能在此处调用delete q;
q->ADT::~ADT();//显示调用析构函数
delete []p;
return 0;
}

注:使用placement new构造起来的对象或数组,要显式调用它们的析构函数来销毁(析构函数并不释放对象的内存),千万不要使用delete.这是因为placement new构造起来的对象或数组大小并不一定等于原来分配的内存大小,使用delete会造成内存泄漏或者之后释放内存时出现运行时错误。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++ new