std::nothrow
std::nothrow
1、在内存不足时,new (std::nothrow)并不抛出异常,而是将指针置NULL。
若不使用std::nothrow,则分配失败时程序直接抛出异常。
2、使用方式:
#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();
}
//... allocation succeeded; continue normally
#include <new>
std::nothrow_t nt;
Task * ptask = new (nt) Task; //user-defined argument
if (!ptask)
//...
4、一种宏实现的方式,取自ACE:
# define ACE_nothrow ::std::nothrow
# define ENOMEM
# define errno (* (ACE_CE_Errno::instance ())) #if defined (ACE_NEW_THROWS_EXCEPTIONS)
# if defined (ACE_HAS_NEW_NOTHROW)
# define ACE_NEW_RETURN(POINTER,CONSTRUCTOR,RET_VAL) \
do { POINTER = new (ACE_nothrow) CONSTRUCTOR; \
if (POINTER == ) { errno = ENOMEM; return RET_VAL; } \
} while ()
# define ACE_NEW(POINTER,CONSTRUCTOR) \
do { POINTER = new(ACE_nothrow) CONSTRUCTOR; \
if (POINTER == ) { errno = ENOMEM; return; } \
} while ()
# define ACE_NEW_NORETURN(POINTER,CONSTRUCTOR) \
do { POINTER = new(ACE_nothrow) CONSTRUCTOR; \
if (POINTER == ) { errno = ENOMEM; } \
} while () # else # define ACE_NEW_RETURN(POINTER,CONSTRUCTOR,RET_VAL) \
do { try { POINTER = new CONSTRUCTOR; } \
catch (ACE_bad_alloc) { ACE_del_bad_alloc errno = ENOMEM; POINTER = ; return RET_VAL; } \
} while () # define ACE_NEW(POINTER,CONSTRUCTOR) \
do { try { POINTER = new CONSTRUCTOR; } \
catch (ACE_bad_alloc) { ACE_del_bad_alloc errno = ENOMEM; POINTER = ; return; } \
} while () # define ACE_NEW_NORETURN(POINTER,CONSTRUCTOR) \
do { try { POINTER = new CONSTRUCTOR; } \
catch (ACE_bad_alloc) { ACE_del_bad_alloc errno = ENOMEM; POINTER = ; } \
} while ()
# endif /* ACE_HAS_NEW_NOTHROW */ #else /* ACE_NEW_THROWS_EXCEPTIONS */ # define ACE_NEW_RETURN(POINTER,CONSTRUCTOR,RET_VAL) \
do { POINTER = new CONSTRUCTOR; \
if (POINTER == ) { errno = ENOMEM; return RET_VAL; } \
} while ()
# define ACE_NEW(POINTER,CONSTRUCTOR) \
do { POINTER = new CONSTRUCTOR; \
if (POINTER == ) { errno = ENOMEM; return; } \
} while ()
# define ACE_NEW_NORETURN(POINTER,CONSTRUCTOR) \
do { POINTER = new CONSTRUCTOR; \
if (POINTER == ) { errno = ENOMEM; } \
} while ()
#endif
使用时:
ACE_Thread_Descriptor *thr_desc = ;
ACE_NEW_RETURN (thr_desc,
ACE_Thread_Descriptor,
-);
std::nothrow的更多相关文章
- std::nothrow 的使用心得
std::nothrow 意思是说,不要跑出异常,改为返回一个nullptr. 一般的使用场景是,建议new的时候使用,避免使用try-catch来捕捉异常. 比如: float m_words = ...
- new (std::nothrow) 与 new
普通new一个异常的类型std::bad_alloc.这个是标准适应性态. 在早期C++的舞台上,这个性态和现在的非常不同:new将返回0来指出一个失败,和malloc()非常相似. 在内存不足时,n ...
- std::sort引发的core
#include <stdio.h> #include <vector> #include <algorithm> #include <new> str ...
- C++ new的nothrow关键字和new_handler用法
C++ new的nothrow关键字和new_handler用法 new && new(std::nothrow) new(std::nothrow) 顾名思义,即不抛出异常,当new ...
- new不抛出异常nothrow与new_handler
可以看这里: http://blog.csdn.net/huyiyang2010/article/details/5984987 现在的new是会抛出异常的,bad::alloc 如果不想抛出异常两种 ...
- 早期malloc分配时,如果内存耗尽分配不出来,会直接返回NULL。现在分配不出来,直接抛出异常(可使用nothrow关键字)
今天和同事review代码时,发现这样的一段代码: Manager * pManager = new Manager(); if(NULL == pManager) { //记录日志 return f ...
- 关于C++中nothrow的某某某
前言 在学习C++中new的种种用法时,在operator new的其中一个重载版本中看一个参数nothrow,想弄清楚到底是什么意思?nothrow顾名思义,就是不抛出的意思嘛!不抛出啥,在C++中 ...
- C++实现DNS域名解析
一.概述 现在来搞定DNS域名解析,其实这是前面一篇文章C++实现Ping里面的遗留问题,要干的活是ping的过程中画红线的部分: cmd下域名解析的命令是nslookup,比如“nslookup w ...
- C++ 代码优化
1.类中所有的属性全部设置为private 然后在需要在外部调用的属性,每个都自己写Get方法返回,或者用Set方法设置 2.类成员变量采用m_前缀,代表类成员 3.采用单例模式 //设置类名为CCo ...
随机推荐
- [转]Android网格视图(GridView)
GridView的一些属性: 1.android:numColumns=”auto_fit” //GridView的列数设置为自动,也可以设置成2.3.4…… 2.android:columnWi ...
- [转]ANDROID开发之SQLite详解
SQLite简介 Google为Andriod的较大的数据处理提供了SQLite,他在数据存储.管理.维护等各方面都相当出色,功能也非常的强大.SQLite具备下列特点: 1.轻量级 使用 SQLit ...
- [转]jquery加载页面的方法(页面加载完成就执行)
jquery加载页面的方法(页面加载完成就执行),建议大家看下windows.onload与$(document).ready之间的区别. 1.$(function(){ $("#a&q ...
- mySql---logback日志写入数据库(mysql)配置
如题 建议将日志级别设置为ERROR.这样可以避免存储过多的数据到数据中. 1 logback 配置文件(如下) <?xml version="1.0" encoding ...
- 基于ARM的射频识别读卡器电路设计
http://tech.yktworld.com/201010/201010032128115666.html 来源:一卡通世界 作者:江小平,李中捷,余晓峰 2010-10-3 ...
- SpringMVC杂记(1) 使用阿里巴巴的fastjson
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...
- 编写第一个Shell脚本
Linux中有好多中不同的shell,bash是linux默认的shell,免费且容易使用. su切换为root权限 1.创建shell脚本 touch hello.sh 2.编辑: vi hello ...
- Eclipse搭建Gradle环境
转自:http://blog.sina.com.cn/s/blog_4b20ae2e0102uz4t.html 1.上Grandle官网下载Gradle,地址:http://www.gradle.or ...
- linux下批量添加新用户
作为一个系统管理员,可能要经常的为机器添加用户,可能是在一台机器上添加很多用户,也有可能是在很多机器上要添加同一个用户. 基本上,批量添加用户有两种方式,一是用useradd + passwd命令配合 ...
- WPF性能调试系列 – 内存监测
WPF性能调试系列文章: WPF页面渲染优化:Application Timeline WPF页面业务加载优化:Ants Performance Profiler WPF内存优化:Ants M ...