std::nothrow

 

std::nothrow

 

在内存不足时,new (std::nothrow)并不抛出异常,而是将指针置NULL。


关于std::nothrow

分类: C++   992人阅读  评论(0)  收藏  举报

http://hi.baidu.com/ilbx/blog/item/30a2c9f550299d2dbd31090f.html


nothrow new与普通new
2009-03-01 16:57
标准的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将按照下面的方式来定义:
 
[cpp]  view plain copy
  1. class nothrow_t // in namespace std  
  2.                  {}; //empty class  
  3. Operator nothrow new is declared like this:  
  4. //declarations from <new>  
  5. void *   operator new (size_t size, const std::nothrow_t &);  
  6. //array version  
  7. void *   operator new[] (size_t size, const std::nothrow_t &);  
  8. In addition, <new> defines a const global object of type nothrow_t:  
  9. extern const nothrow_t nothrow//in namespace std  


   按照这个方式,调用nothrow new的代码将可以使用统一的变量名字。比如:
[cpp]  view plain copy
  1. #include <new>  
  2. #include <iostream> // for std::cerr  
  3. #include <cstdlib> // for std::exit()  
  4. Task * ptask = new (std::nothrow) Task;  
  5. if (!ptask)  
  6. {  
  7. std::cerr<<"allocation failure!";  
  8. std::exit(1);  
  9. }  
  10. //... allocation succeeded; continue normally   


但是,你可以注意到你创建了你自己的nothrow_t对象来完成相同的效应:

[cpp]  view plain copy
  1. #include <new>  
  2. std::nothrow_t nt;  
  3. Task * ptask = new (nt) Task; //user-defined argument  
  4. if (!ptask)  
  5. //...   

分配失败是非常普通的,它们通常在植入性和不支持异常的可移动的器件中发生更频繁。因此,应用程序开发者在这个环境中使用nothrow new来替代普通的new是非常安全的。



http://www.cplusplus.com/reference/std/new/nothrow/


extern const nothrow_t nothrow;

Nothrow constant

This constant value is used as an argument for  operator new  and  operator new[]  to indicate that these functions shall not throw an exception on failure, but return a null pointer instead.

By default, when the new operator is used to allocate memory and the handling function is unable to do so, abad_alloc exception is thrown. But when nothrow is used as argument for new, it returns a null pointer instead.

This constant ( nothrow ) is just a value of type  nothrow_t , which only purpose is to trigger an overloaded version of function  operator new  or  operator new[]  that takes an argument of this type.

In C++, the  new  operator can be overloaded to take more than one parameter: The first parameter passed to the  operator new  function is always the size of the element type to be allocated, but more arguments can be passed to this function by enclosing them in parentheses. For example:

[cpp]  view plain copy
  1. int * p = new (x) int;  

is a valid expression that, at some point, calls:

[cpp]  view plain copy
  1. operator new (sizeof(int),x);  

By default, one of the versions of  operator new  is overloaded to accept a parameter of type  nothrow_t  (like nothrow ). The value itself is not used, but that version of  operator new  shall return zero in case of failure instead of throwing an exception.

The same applies for operator  new[]  and function  operator new[]  .

Example

[cpp]  view plain copy
  1. // nothrow example  
  2. #include <iostream>  
  3. #include <new>  
  4. using namespace std;  
  5.   
  6. int main () {  
  7.   cout << "Attempting to allocate 1 MiB...";  
  8.   char* p = new (nothrowchar [1048576];  
  9.   if (p==0) cout << "Failed!\n";  
  10.   else {  
  11.     cout << "Success!\n";  
  12.     delete[] p;  
  13.   }  
  14.   return 0;  
  15. }  

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值