{
if (s_pHttpClient == nullptr) {
s_pHttpClient = new (std::nothrow) HttpClient();
}
return s_pHttpClient;
}
標準的new頭文件可以定義普通的new,同時,它也可以定義一個變體new操作符,這個操作符叫做nothrownew。普通的new:過去和現在 普通new一個異常的類型std::bad_alloc。這個是標準適應性態。在早期C++的舞臺上,這個性態和現在的非常不同;new將返回0來指出一個失敗,和malloc()非常相似。
在一定的環境下,返回一個NULL指針來表示一個失敗依然是一個不錯的選擇。C++標準委員會意識到這個問題,所以他們決定定義一個特別的new操作符版本,這個版本返回0表示失敗。
一個nothow new語句和普通的new語句相似,除了它的變量將涉及到std::nothrow_t。Class std::nothrow_t在new將按照下面的方式來定義:
- class nothrow_t // in namespace std
- {}; //empty class
- Operator nothrow new is declared like this:
- //declarations from <new>
- void * operator new (size_t size, const std::nothrow_t &);
- //array version
- void * operator new[] (size_t size, const std::nothrow_t &);
- In addition, <new> defines a const global object of type nothrow_t:
- extern const nothrow_t nothrow; //in namespace std
- #include <new>
- #include <iostream> // for std::cerr
- #include <cstdlib> // for std::exit()
- Task * ptask = new (std::nothrow) Task;
- if (!ptask)
- {
- std::cerr<<"allocation failure!";
- std::exit(1);
- }
詳細:
一. 簡介
new有三種使用方式:plain new,nothrow new和placement new。
(1)plain 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是徒勞的。
(2)nothrow new是不拋出異常的運算符new的形式。nothrow new在失敗時,返回NULL。定義如下:
void * operator new(std::size_t,const std::nothrow_t&) throw();
void operator delete(void*) throw();
(3)placement new意即“放置”,這種new允許在一塊已經分配成功的內存上重新構造對象或對象數組。placement new不用擔心內存分配失敗,因爲它根本不分配內存,它做的唯一一件事情就是調用對象的構造函數。定義如下:
void* operator new(size_t,void*);
void operator delete(void*,void*);
提示1:palcement new的主要用途就是反覆使用一塊較大的動態分配的內存來構造不同類型的對象或者他們的數組。
提示2:placement new構造起來的對象或其數組,要顯示的調用他們的析構函數來銷燬,千萬不要使用delete。
char* p = new(nothrow) char[100];
long *q1 = new(p) long(100);
int *q2 = new(p) int[100/sizeof(int)];
二.實例
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;
}