<small id='44j9J'></small><noframes id='44j9J'>

      <bdo id='44j9J'></bdo><ul id='44j9J'></ul>

      <legend id='44j9J'><style id='44j9J'><dir id='44j9J'><q id='44j9J'></q></dir></style></legend>

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

    1. <tfoot id='44j9J'></tfoot>
    2. 如何处理或避免 C++ 中的堆栈溢出

      时间:2023-08-28
        <i id='6eQOC'><tr id='6eQOC'><dt id='6eQOC'><q id='6eQOC'><span id='6eQOC'><b id='6eQOC'><form id='6eQOC'><ins id='6eQOC'></ins><ul id='6eQOC'></ul><sub id='6eQOC'></sub></form><legend id='6eQOC'></legend><bdo id='6eQOC'><pre id='6eQOC'><center id='6eQOC'></center></pre></bdo></b><th id='6eQOC'></th></span></q></dt></tr></i><div id='6eQOC'><tfoot id='6eQOC'></tfoot><dl id='6eQOC'><fieldset id='6eQOC'></fieldset></dl></div>
      1. <small id='6eQOC'></small><noframes id='6eQOC'>

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

                <tfoot id='6eQOC'></tfoot>
                本文介绍了如何处理或避免 C++ 中的堆栈溢出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                在 C++ 中,堆栈溢出通常会导致程序不可恢复的崩溃.对于需要真正健壮的程序,这是一种不可接受的行为,特别是因为堆栈大小是有限的.关于如何处理问题的几个问题.

                In C++ a stack overflow usually leads to an unrecoverable crash of the program. For programs that need to be really robust, this is an unacceptable behaviour, particularly because stack size is limited. A few questions about how to handle the problem.

                1. 有没有办法通过通用技术来防止堆栈溢出.(一个可扩展、强大的解决方案,包括处理消耗大量堆栈的外部库等)

                1. Is there a way to prevent stack overflow by a general technique. (A scalable, robust solution, that includes dealing with external libraries eating a lot of stack, etc.)

                如果发生堆栈溢出,有没有办法处理它们?最好是,在有一个处理程序来处理那个问题之前,堆栈会被解开.

                Is there a way to handle stack overflows in case they occur? Preferably, the stack gets unwound until there's a handler to deal with that kinda issue.

                有些语言具有可扩展堆栈的线程.在 C++ 中可以实现类似的功能吗?

                There are languages out there, that have threads with expandable stacks. Is something like that possible in C++?

                对 C++ 行为的解决方案的任何其他有用的评论将不胜感激.

                Any other helpful comments on the solution of the C++ behaviour would be appreciated.

                推荐答案

                处理堆栈溢出不是正确的解决方案,相反,您必须确保您的程序不会溢出堆栈.

                Handling a stack overflow is not the right solution, instead, you must ensure that your program does not overflow the stack.

                不要在堆栈上分配大变量(什么是大"取决于程序).确保任何递归算法在已知最大深度后终止.如果递归算法可能递归未知次数或大量次数,请自行管理递归(通过维护自己的动态分配堆栈)或将递归算法转换为等效的迭代算法

                Do not allocate large variables on the stack (where what is "large" depends on the program). Ensure that any recursive algorithm terminates after a known maximum depth. If a recursive algorithm may recurse an unknown number of times or a large number of times, either manage the recursion yourself (by maintaining your own dynamically allocated stack) or transform the recursive algorithm into an equivalent iterative algorithm

                必须非常健壮"的程序不会使用占用大量堆栈"的第三方或外部库.

                A program that must be "really robust" will not use third-party or external libraries that "eat a lot of stack."

                请注意,某些平台确实会在发生堆栈溢出时通知程序并允许程序处理错误.例如,在 Windows 上,会引发异常.此异常不是 C++ 异常,但它是异步异常.C++ 异常只能由 throw 语句抛出,而异步异常可能在程序执行过程中的任何时候抛出.不过,这是意料之中的,因为堆栈溢出随时可能发生:任何函数调用或堆栈分配都可能使堆栈溢出.

                Note that some platforms do notify a program when a stack overflow occurs and allow the program to handle the error. On Windows, for example, an exception is thrown. This exception is not a C++ exception, though, it is an asynchronous exception. Whereas a C++ exception can only be thrown by a throw statement, an asynchronous exception may be thrown at any time during the execution of a program. This is expected, though, because a stack overflow can occur at any time: any function call or stack allocation may overflow the stack.

                问题是堆栈溢出可能导致异步异常被抛出,即使是从不期望抛出任何异常的代码(例如,来自标记为 noexceptthrow() 的函数) 在 C++ 中).因此,即使您确实以某种方式处理了此异常,您也无法知道您的程序是否处于安全状态.因此,处理异步异常的最好方法是根本不处理它(*).如果抛出一个,则表示程序存在错误.

                The problem is that a stack overflow may cause an asynchronous exception to be thrown even from code that is not expected to throw any exceptions (e.g., from functions marked noexcept or throw() in C++). So, even if you do handle this exception somehow, you have no way of knowing that your program is in a safe state. Therefore, the best way to handle an asynchronous exception is not to handle it at all(*). If one is thrown, it means the program contains a bug.

                其他平台可能有类似的方法来处理"堆栈溢出错误,但任何此类方法都可能遇到相同的问题:预期不会导致错误的代码可能会导致错误.

                Other platforms may have similar methods for "handling" a stack overflow error, but any such methods are likely to suffer from the same problem: code that is expected not to cause an error may cause an error.

                (*) 有一些非常罕见的例外.

                这篇关于如何处理或避免 C++ 中的堆栈溢出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:整数的用户输入 - 错误处理 下一篇:std::cin 不会在错误输入时引发异常

                相关文章

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

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

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

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