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 ...
随机推荐
- tensorflow LSTM+CTC使用详解
最近用tensorflow写了个OCR的程序,在实现的过程中,发现自己还是跳了不少坑,在这里做一个记录,便于以后回忆.主要的内容有lstm+ctc具体的输入输出,以及TF中的CTC和百度开源的warp ...
- 使用unsafe.Pointer将结构体转为[]byte
package main import ( "fmt" "unsafe" ) type TestStructTobytes struct { data int6 ...
- 微信公众号调用外部浏览器打开指定URL链接是如何实现的
在涉及移动端支付的项目时,由于对支付需求的精细化,不仅需要扫码支付,还有唤醒App支付,另外还有在微信.QQ.支付宝内置浏览器给出相应的提示. 好在国内各大巨头公司在开发浏览器的时候都在浏览器标识上加 ...
- learning java Date类
var d1 = new Date(); var d2 = new Date(System.currentTimeMillis() + 1000); System.out.println(d1); S ...
- 鼠标常用样式(cursor)
<body> <div>常用的鼠标样式(cursor):pointer,move,defalt,text(火狐不支持hand)</div> </body> ...
- git 查看项目代码统计命令
git log --author="xxxxxxxx" --pretty=tformat: --numstat | gawk '{ add += $1 ; subs += $2 ; ...
- Day11:Flex布局
参考: 来源:http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html 网页布局是css的一个重点. 盒子模型 display属性 positi ...
- TCP的几个知识点
1. 三次握手.四次挥手 详细查看:https://www.cnblogs.com/amiezhang/p/6703390.html 2. ARQ 协议 ARQ 就是超时重传机制,分为 2 种:停止等 ...
- 浅谈bitset
维护二进制的数据结构,常数可近似看作\(\frac{1}{32}\) 定义 bitset<4> bitset1; 长度为4,下标[0,3],默认为0 bitset<4> bit ...
- AspNetCore3.0 和 JWT
添加NuGet引用 IdentityModel Microsoft.AspNetCore.Authorization.JwtBearer 在appsettings.json中添加JwtBearer配置 ...