normal new,nothrow new和placement new
转载链接: https://blog.csdn.net/u010781856/article/details/44996137
1、normal new 名思义就是普通的new,就是我们惯常使用的new。在C++中是这样定义的:
void* operator new(std::size_t) throw(std::bad_alloc);
void operator delete(void *) throw();
提示:plain new在分配失败的情况下,抛出异常std::bad_alloc而不是返回NULL,因此通过判断返回值是否为NULL是徒劳的。
代码示例:
#include "stdafx.h" #include <iostream> using namespace std; void *operator new(std::size_t) throw(std::bad_alloc); void operator delete(void*) throw(); 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)//标准C++ plain new失败后抛出标准异常std::bad_alloc而非返回NULL,因此检查返回值是否为NULL判断分配是否成功是徒劳的。 cout << "failure" << endl; delete[] p; } catch (const std::bad_alloc &ex) { cout << ex.what() << endl; } return 0; }
2、nothrow new 是不抛出异常的运算符new的形式。nothrow new在失败时,返回NULL。定义如下:
void * operator new(std::size_t,const std::nothrow_t&) throw();
void operator delete(void*) throw();
代码示例:
// 定义代码如下 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 意即“放置”,这种new允许在一块已经分配成功的内存上重新构造对象或对象数组。placement new不用担心内存分配失败,因为它根本不分配内存,它做的唯一一件事情就是调用对象的构造函数。定义如下:
void* operator new(size_t,void*);
void operator delete(void*,void*);
提示1:palcement new的主要用途就是反复使用一块较大的动态分配的内存来构造不同类型的对象或者他们的数组。
提示2: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会造成内存泄漏或者之后释放内存时出现运行时错误。