<i id='5ta2n'><tr id='5ta2n'><dt id='5ta2n'><q id='5ta2n'><span id='5ta2n'><b id='5ta2n'><form id='5ta2n'><ins id='5ta2n'></ins><ul id='5ta2n'></ul><sub id='5ta2n'></sub></form><legend id='5ta2n'></legend><bdo id='5ta2n'><pre id='5ta2n'><center id='5ta2n'></center></pre></bdo></b><th id='5ta2n'></th></span></q></dt></tr></i><div id='5ta2n'><tfoot id='5ta2n'></tfoot><dl id='5ta2n'><fieldset id='5ta2n'></fieldset></dl></div>
      <bdo id='5ta2n'></bdo><ul id='5ta2n'></ul>

      <small id='5ta2n'></small><noframes id='5ta2n'>

        <tfoot id='5ta2n'></tfoot>

      1. <legend id='5ta2n'><style id='5ta2n'><dir id='5ta2n'><q id='5ta2n'></q></dir></style></legend>

        在任何情况下 new 都会返回 NULL 吗?

        时间:2023-10-07

            <legend id='fFeNO'><style id='fFeNO'><dir id='fFeNO'><q id='fFeNO'></q></dir></style></legend>
          1. <tfoot id='fFeNO'></tfoot>
            • <bdo id='fFeNO'></bdo><ul id='fFeNO'></ul>
                <tbody id='fFeNO'></tbody>

                • <small id='fFeNO'></small><noframes id='fFeNO'>

                  <i id='fFeNO'><tr id='fFeNO'><dt id='fFeNO'><q id='fFeNO'><span id='fFeNO'><b id='fFeNO'><form id='fFeNO'><ins id='fFeNO'></ins><ul id='fFeNO'></ul><sub id='fFeNO'></sub></form><legend id='fFeNO'></legend><bdo id='fFeNO'><pre id='fFeNO'><center id='fFeNO'></center></pre></bdo></b><th id='fFeNO'></th></span></q></dt></tr></i><div id='fFeNO'><tfoot id='fFeNO'></tfoot><dl id='fFeNO'><fieldset id='fFeNO'></fieldset></dl></div>

                  本文介绍了在任何情况下 new 都会返回 NULL 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我知道根据 C++ 标准,如果 new 无法分配内存,它应该抛出 std::bad_alloc 异常.但我听说有些编译器如 VC6(或 CRT 实现?)不遵守它.这是真的 ?我问这个是因为在每个新语句之后检查 NULL 会使代码看起来非常难看.

                  I know that according to C++ standard in case the new fails to allocate memory it is supposed to throw std::bad_alloc exception. But I have heard that some compilers such as VC6 (or CRT implementation?) do not adhere to it. Is this true ? I am asking this because checking for NULL after each and every new statement makes code look very ugly.

                  推荐答案

                  VC6 在这方面默认是不合规的.VC6的new返回0(或NULL).

                  VC6 was non-compliant by default in this regard. VC6's new returned 0 (or NULL).

                  这是微软关于这个问题的知识库文章以及他们建议的使用自定义 new 处理程序的解决方法:

                  Here's Microsoft's KB Article on this issue along with their suggested workaround using a custom new handler:

                  • 在 Visual C++ 中,Operator new 不会在失败时引发 bad_alloc 异常

                  如果您有为 VC6 行为编写的旧代码,您可以通过链接名为 nothrownew.obj 的对象文件,使用较新的 MSVC 编译器(例如 7.0 及更高版本)获得相同的行为.实际上有一个相当复杂的一组规则7.0 和 7.1 编译器(VS2002 和 VS2003)来确定它们是默认不抛出还是抛出 new.

                  If you have old code that was written for VC6 behavior, you can get that same behavior with newer MSVC compilers (something like 7.0 and later) by linking in a object file named nothrownew.obj. There's actually a fairly complicated set of rules in the 7.0 and 7.1 compilers (VS2002 and VS2003) to determine whether they defaulted to non-throwing or throwing new.

                  似乎 MS 在 8.0 (VS2005) 中对此进行了清理—现在它总是默认为抛出新的,除非你特别链接到 nothrownew.obj.

                  It seems that MS cleaned this up in 8.0 (VS2005)—now it always defaults to a throwing new unless you specifically link to nothrownew.obj.

                  请注意,您可以指定您希望 new 返回 0 而不是使用 std 抛出 std::bad_alloc::nothrow 参数:

                  Note that you can specify that you want new to return 0 instead of throwing std::bad_alloc using the std::nothrow parameter:

                  SomeType *p = new(std::nothrow) SomeType;
                  

                  这似乎在 VC6 中有效,因此它可能是一种或多或少机械地修复代码的方法,以便在所有编译器中都能正常工作,这样您就不必重新处理现有的错误处理.

                  This appears to work in VC6, so it could be a way to more or less mechanically fix the code to work the same with all compilers so you don't have to rework existing error handling.

                  这篇关于在任何情况下 new 都会返回 NULL 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:设置标准流使用的内部缓冲区 (pubsetbuf) 下一篇:MSVC++ 可变参数宏扩展

                  相关文章

                  最新文章

                  <legend id='vZ2QG'><style id='vZ2QG'><dir id='vZ2QG'><q id='vZ2QG'></q></dir></style></legend>

                  <small id='vZ2QG'></small><noframes id='vZ2QG'>

                • <tfoot id='vZ2QG'></tfoot>

                      <bdo id='vZ2QG'></bdo><ul id='vZ2QG'></ul>
                    <i id='vZ2QG'><tr id='vZ2QG'><dt id='vZ2QG'><q id='vZ2QG'><span id='vZ2QG'><b id='vZ2QG'><form id='vZ2QG'><ins id='vZ2QG'></ins><ul id='vZ2QG'></ul><sub id='vZ2QG'></sub></form><legend id='vZ2QG'></legend><bdo id='vZ2QG'><pre id='vZ2QG'><center id='vZ2QG'></center></pre></bdo></b><th id='vZ2QG'></th></span></q></dt></tr></i><div id='vZ2QG'><tfoot id='vZ2QG'></tfoot><dl id='vZ2QG'><fieldset id='vZ2QG'></fieldset></dl></div>