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 Studio --> Gradle Build设置自动
ps:http://www.cnblogs.com/kangyi/p/4448398.html 应用场景 通常情况下我们的apps发布后也就是release模式下log是不显示的,debug模式下是显 ...
- 第2篇--JVM的内存区域划分
学过C语言的朋友都知道C编译器在划分内存区域的时候经常将管理的区域划分为数据段和代码段,数据段包括堆.栈以及静态数据区.那么在Java语言当中,内存又是如何划分的呢? 由于Java程序是交由JVM执行 ...
- Problem G: 切煎饼
Description 王小二自夸刀工不错,有人放一张大的圆煎饼在砧板上,问他:饼不允许离开砧板,切100刀最多能切多少块? Input 多组测试数据,每组输入1个整数,代表切的刀数 Output 每 ...
- ajax请求数据填充表格———使用art-template模板提高效率
一.为什么要用art-template模板 在实习的一次项目中,因为需求中展示表格的字段有很多个,后端返回的也是json数据,这时候如果还是使用之前的字符串拼接,这样会开发得比较慢,而且容易出错,而且 ...
- Spring---介绍
核心容器:Core.Beans.Context.EL模块 1. Core模块:封装了框架依赖的最底层部分,包括访问资源.类型转换及一些常用工具类 2. Beans模块:提供了框架的基础 ...
- python开发_bisect
现在有如下的需求: ''' 实现这样的一个功能: 对一个班级的学生的成绩做出一些评定,评定规则是: one: [0-60) -- F two: [60-70) -- D three: [70-80) ...
- Acdream 1738 世风日下的哗啦啦族I 树套树
世风日下的哗啦啦族I Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acdream.info/problem?pid=1738 Descri ...
- 在Windows下将Redis注册为本地服务
当前redis版本:3.2.100 通常情况下我们可以通过 redis-server.exe 和配置文件启动redis服务 : redis-server.exe redis.windows.conf ...
- JAVA容器-模拟LinkedList实现(双链表)
概述 LinkedList实质上就是双向链表的拓展的实现,我们将关注一下问题.LinkedList 1.双向链表怎么来实现插入.删除.查询? 2.利用二分法提高查询效率. 3.不同步,线程不安全,需要 ...
- JAVA EE 博客实例
http://www.cnblogs.com/hoojo/category/276244.html