<legend id='awai9'><style id='awai9'><dir id='awai9'><q id='awai9'></q></dir></style></legend>
    <tfoot id='awai9'></tfoot>

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

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

      新 (std::nothrow) 与 try/catch 块中的新

      时间:2024-08-13

        <bdo id='xIft3'></bdo><ul id='xIft3'></ul>

        <tfoot id='xIft3'></tfoot>

            <tbody id='xIft3'></tbody>
        • <legend id='xIft3'><style id='xIft3'><dir id='xIft3'><q id='xIft3'></q></dir></style></legend>
            <i id='xIft3'><tr id='xIft3'><dt id='xIft3'><q id='xIft3'><span id='xIft3'><b id='xIft3'><form id='xIft3'><ins id='xIft3'></ins><ul id='xIft3'></ul><sub id='xIft3'></sub></form><legend id='xIft3'></legend><bdo id='xIft3'><pre id='xIft3'><center id='xIft3'></center></pre></bdo></b><th id='xIft3'></th></span></q></dt></tr></i><div id='xIft3'><tfoot id='xIft3'></tfoot><dl id='xIft3'><fieldset id='xIft3'></fieldset></dl></div>
          1. <small id='xIft3'></small><noframes id='xIft3'>

                本文介绍了新 (std::nothrow) 与 try/catch 块中的新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我在学习new之后做了一些研究,不像我习惯的malloc(),分配失败时不会返回NULL,发现有两种不同的方式检查 new 是否成功.这两种方式是:

                I did some research after learning new, unlike malloc() which I am used to, does not return NULL for failed allocations, and found there are two distinct ways of checking whether new had succeeded or not. Those two ways are:

                try
                {
                    ptr = new int[1024];
                }
                catch(std::bad_alloc& exc)
                {
                    assert();
                };
                

                ptr = new (std::nothrow) int[1024];
                if(ptr == NULL) 
                    assert();
                

                我相信这两种方法可以实现相同的目标,(当然,如果我错了,请纠正我!),所以我的问题是:

                I believe the two ways accomplish the same goal, (correct me if I am wrong of course!), so my question is this:

                这是检查 new 是否成功的更好选择,完全基于可读性、可维护性和性能,同时忽略事实上的 c++ 编程约定.

                which is the better option for checking if new succeeded, based entirely on readability, maintainability, and performance, while disregarding de-facto c++ programming convention.

                推荐答案

                考虑一下你在做什么.你正在分配内存.如果由于某种原因内存分配不能工作,你assert.如果您只是让 std::bad_alloc 传播回 main,或多或少会发生什么.在发布版本中,assert 是无操作的,您的程序在尝试访问内存时会崩溃.所以这和让异常冒泡一样:停止应用程序.

                Consider what you are doing. You're allocating memory. And if for some reason memory allocation cannot work, you assert. Which is more or less exactly what will happen if you just let the std::bad_alloc propagate back to main. In a release build, where assert is a no-op, your program will crash when it tries to access the memory. So it's the same as letting the exception bubble up: halting the app.

                所以问自己一个问题:你真的需要关心如果你的内存用完会发生什么吗?如果你所做的只是断言,那么异常方法会更好,因为它不会用随机的 assert 来混淆你的代码.您只需让异常回退到 main.

                So ask yourself a question: Do you really need to care what happens if you run out of memory? If all you're doing is asserting, then the exception method is better, because it doesn't clutter your code with random asserts. You just let the exception fall back to main.

                如果您在无法分配内存的情况下确实有一个特殊的代码路径(也就是说,您实际上可以继续运行),那么异常可能是也可能不是,这取决于代码路径是什么.如果代码路径只是一个通过将指针设为空来设置的开关,那么 nothrow 版本会更简单.如果相反,您需要做一些完全不同的事情(从静态缓冲区中拉取,或删除一些东西,或其他什么),那么捕获 std::bad_alloc 非常好.

                If you do in fact have a special codepath in the event that you cannot allocate memory (that is, you can actually continue to function), exceptions may or may not be a way to go, depending on what the codepath is. If the codepath is just a switch set by having a pointer be null, then the nothrow version will be simpler. If instead, you need to do something rather different (pull from a static buffer, or delete some stuff, or whatever), then catching std::bad_alloc is quite good.

                这篇关于新 (std::nothrow) 与 try/catch 块中的新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:从信号处理程序中引发异常 下一篇:捕捉“堆栈溢出"递归 C++ 函数中的异常

                相关文章

                  <tfoot id='7pPZ2'></tfoot>

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

                    <small id='7pPZ2'></small><noframes id='7pPZ2'>

                    <legend id='7pPZ2'><style id='7pPZ2'><dir id='7pPZ2'><q id='7pPZ2'></q></dir></style></legend>