new (std::nothrow) 与 new
普通new一个异常的类型std::bad_alloc。这个是标准适应性态。
在早期C++的舞台上,这个性态和现在的非常不同;new将返回0来指出一个失败,和malloc()非常相似。
在内存不足时,new (std::nothrow)并不抛出异常,而是将指针置NULL。
在一定的环境下,返回一个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
按照这个方式,调用nothrow new的代码将可以使用统一的变量名字。比如:
#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);
}
//... allocation succeeded; continue normally
但是,你可以注意到你创建了你自己的nothrow_t对象来完成相同的效应:
#include <new>
std::nothrow_t nt;
Task * ptask = new (nt) Task; //user-defined argument
if (!ptask)
//...
分配失败是非常普通的,它们通常在植入性和不支持异常的可移动的器件中发生更频繁。因此,应用程序开发者在这个环境中使用nothrow new来替代普通的new是非常安全的。
new (std::nothrow) 与 new的更多相关文章
- std::nothrow 的使用心得
std::nothrow 意思是说,不要跑出异常,改为返回一个nullptr. 一般的使用场景是,建议new的时候使用,避免使用try-catch来捕捉异常. 比如: float m_words = ...
- std::nothrow
std::nothrow 1.在内存不足时,new (std::nothrow)并不抛出异常,而是将指针置NULL. 若不使用std::nothrow,则分配失败时程序直接抛出异常. 2.使用方式: ...
- 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 ...
随机推荐
- GBDT(梯度提升树) 原理小结
在之前博客中,我们对Boosting家族的Adaboost算法做了总结,本文就对Boosting家族中另一个重要的算法梯度提升树(Gradient Boosting Decison Tree, 以下简 ...
- logstash-output-jdbc遇到connection is not available,request time out after 10000ms的问题解决
上一篇logstash-output-jdbc使用中提到“运行bin/logstash -f test.conf时可能提示注册插件失败”,通过分析详细的错误日志,发现其赫然写着“connection ...
- 错误信息: The server cannot or will not process the request due to something that is perceived to be a client error
错误原因:在提交的表单中有 date 类型的数据,也就是不能传输日期类型的数据. 嗯!我知道,去吧!
- [转载]JSON WEB TOKEN,简单谈谈TOKEN的使用及在C#中的实现
https://www.cnblogs.com/chenwolong/p/Token.html
- .net大文件分块上传断点续传demo
IE的自带下载功能中没有断点续传功能,要实现断点续传功能,需要用到HTTP协议中鲜为人知的几个响应头和请求头. 一. 两个必要响应头Accept-Ranges.ETag 客户端每次提交下载请求时,服务 ...
- learning java AWT 布局管理器FlowLayout
AWT提供了FlowLayout 从左到右排列所有组件,遇到边界就会折回下一行重新开始. import java.awt.*; public class FlowLayoutTest { publ ...
- C++函数调用方式约定stdcall,cdecl,pascal,naked,thiscall,fastcall
https://www.cnblogs.com/xiangtingshen/p/11014514.html C++函数调用约定_cdecl约定:参数:从右向左依次入栈堆栈平衡:调用方平衡 #inclu ...
- (24)打鸡儿教你Vue.js
学习Vue基础语法 Vue中的组件 Vue-cli的使用 1.使用Vue2.0版本实现响应式编程 2.理解Vue编程理念与直接操作Dom的差异 3.Vue常用的基础语法 4.使用Vue编写TodoLi ...
- Bat 复制本地文件到共享目录
@echo off title "copy UI" net use \\172.16.104.93\心电图 "password" /user:"adm ...
- js中array.some()的用法
let array=[ { name:'jack', age:'19' }, { name:'rose', age:'19' } ] var box=array.some((value,index)= ...