Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 关闭C++;全局新运算符异常_C++_Windows_Visual Studio 2010_Exception_Std - Fatal编程技术网

C++ 关闭C++;全局新运算符异常

C++ 关闭C++;全局新运算符异常,c++,windows,visual-studio-2010,exception,std,C++,Windows,Visual Studio 2010,Exception,Std,是否有任何方法可以全局关闭新操作员的异常? 如果不止一个,哪一个是最好的 我试过这个,但我真的不确定: #include <new> using std::nothrow; #包括 使用std::nothrow; 我试着用谷歌搜索“usingstd::nothrow;”,但没有结果 我正在使用MSVC2010 当然我知道new(std::nothrow)myClass()否。这会破坏很多代码,例如标准标题中的代码,它确实依赖于new抛出 < C++委员会意识到,通过在一个名称下标

是否有任何方法可以全局关闭新操作员的异常? 如果不止一个,哪一个是最好的

我试过这个,但我真的不确定:

#include <new>
using std::nothrow;
#包括
使用std::nothrow;
我试着用谷歌搜索“usingstd::nothrow;”,但没有结果

我正在使用MSVC2010


当然我知道
new(std::nothrow)myClass()

否。这会破坏很多代码,例如标准标题中的代码,它确实依赖于
new
抛出

< C++委员会意识到,通过在一个名称下标准化几十种几乎兼容的语言带来的危险,只有5种这样的选项,您已经有32种不兼容语言。p>
#define NEW1(type, ...) new (std::nothrow) type(__VA_ARGS__)
#define NEW(type, size, ...) new (std::nothrow) type[size](__VA_ARGS__)
//用法:

int *a=NEW1(int),     //single non-initialized int
    *b=NEW1(int, 42), //single int with value 42
    *c=NEW(int, 42);  //array of ints made of 42 elements

delete a;
delete b;
delete[] c;

你好请不要推荐使用宏来编写代码的解决方案,除非问题特别要求这样做。按照您的建议,创建使用
NEW
NEW1
的客户机代码,这将强制您的整个代码库在项目生命周期内支持这些宏。“只需创建一个宏”的想法会造成错误(并永久性地增加理解整个代码库所需的工作量)。这是一个有趣而棘手的解决方案。但是是的,我不能把它添加到我的项目中。。。据我所知,最好的方法是避免使用宏。根据定义,这不会全局关闭异常,而只会在使用宏时关闭异常。宏在显式指定
std::nothrow
上没有添加任何值,但它确实给维护程序员带来了困惑。我删除了它。我主要考虑了第一部分,很抱歉我的想法不起作用。通过设置新的处理程序,您可以确保永远不会得到
std::bad_alloc
。但是,当没有更多内存时,此函数不能导致
操作员new
返回
nullptr
。仅当它提供了更多可用内存时才允许返回;否则,就会出现您所说的问题:所有标准库都将崩溃。(大多数应用程序可能都应该安装一个新的处理程序;除非您有从
错误中恢复的真正策略,否则最好立即终止程序。)