普通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的更多相关文章

  1. std::nothrow 的使用心得

    std::nothrow 意思是说,不要跑出异常,改为返回一个nullptr. 一般的使用场景是,建议new的时候使用,避免使用try-catch来捕捉异常. 比如: float m_words = ...

  2. std::nothrow

    std::nothrow 1.在内存不足时,new (std::nothrow)并不抛出异常,而是将指针置NULL. 若不使用std::nothrow,则分配失败时程序直接抛出异常. 2.使用方式: ...

  3. std::sort引发的core

    #include <stdio.h> #include <vector> #include <algorithm> #include <new> str ...

  4. C++ new的nothrow关键字和new_handler用法

    C++ new的nothrow关键字和new_handler用法 new && new(std::nothrow) new(std::nothrow) 顾名思义,即不抛出异常,当new ...

  5. new不抛出异常nothrow与new_handler

    可以看这里: http://blog.csdn.net/huyiyang2010/article/details/5984987 现在的new是会抛出异常的,bad::alloc 如果不想抛出异常两种 ...

  6. 早期malloc分配时,如果内存耗尽分配不出来,会直接返回NULL。现在分配不出来,直接抛出异常(可使用nothrow关键字)

    今天和同事review代码时,发现这样的一段代码: Manager * pManager = new Manager(); if(NULL == pManager) { //记录日志 return f ...

  7. 关于C++中nothrow的某某某

    前言 在学习C++中new的种种用法时,在operator new的其中一个重载版本中看一个参数nothrow,想弄清楚到底是什么意思?nothrow顾名思义,就是不抛出的意思嘛!不抛出啥,在C++中 ...

  8. C++实现DNS域名解析

    一.概述 现在来搞定DNS域名解析,其实这是前面一篇文章C++实现Ping里面的遗留问题,要干的活是ping的过程中画红线的部分: cmd下域名解析的命令是nslookup,比如“nslookup w ...

  9. C++ 代码优化

    1.类中所有的属性全部设置为private 然后在需要在外部调用的属性,每个都自己写Get方法返回,或者用Set方法设置 2.类成员变量采用m_前缀,代表类成员 3.采用单例模式 //设置类名为CCo ...

随机推荐

  1. python同时取每个列表的第一个元素

    在实际爬虫开发中, 经常用到列表保存数据, 在使用这些数据的时候,需要要取每个列表里的第一个元素进行拼接. 就需要用到python的内置方法:“zip()" # 现在有3个列表:li_1, ...

  2. HTML锚点控制,跳转页面后定位到相应位置

    想在点击更多的页面 跳转后  用户能看到的是新闻  不用再用scollbar拖下来到新闻页面  这时候就需要在链接上 做下处理 <a href="/article/list/page/ ...

  3. 浏览器在线预览pdf、txt、office文件

    //使用文件预览的原因是:TMD微信浏览器屏蔽掉文件下载链接,只好折中使用文件在线预览功能//要点:1.office文件用微软的插件打开 http://view.officeapps.live.com ...

  4. win32线程栈溢出问题 (二)

    3.2.函数递归调用引发的栈溢出 写一段最简单的无穷递归代码,如下: #include "stdafx.h" void f(void) { f(); } int _tmain(in ...

  5. ES安装手册

    http://www.elastic.co/guide/en/elasticsearch/reference/current/setup-configuration.htmlhttps://githu ...

  6. Nginx 安装配置【必须把文件到放到机器上】

    [必须把所有下载的gz文件到放到机器上:编译] 1.安装nginx之前的编译软件 yum -y install make zlib zlib-devel gcc-c++ libtool  openss ...

  7. VIJOS PID221 / 烦人的幻灯片

     暴力出奇迹,学长诚不欺我. PID221 / 烦人的幻灯片 2017-04-14 19:47:08 运行耗时:30 ms 运行内存:12292 KB 查看最后一次评测记录 题目描述 李教授于今天下午 ...

  8. [golang]text/template模板

    这个可以用来处理text文本,不过我更偏爱做成代码生成器. [golang]text/template模板 package main import ( "os" "tex ...

  9. mac eclipse 创建Java 工程

    首先创建Java工程testjavapro,创建包testjavapro,接着创建类testjava 参考: https://www.jianshu.com/p/20280b850c95

  10. spark-shell启动错误

    18/06/24 16:41:40 ERROR spark.SparkContext: Error initializing SparkContext.java.net.BindException: ...