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

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

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

      1. 捕获“堆栈溢出"递归 C++ 函数中的异常

        时间:2023-08-03

      2. <tfoot id='YkMjn'></tfoot>

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

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

                <tbody id='YkMjn'></tbody>
            • <i id='YkMjn'><tr id='YkMjn'><dt id='YkMjn'><q id='YkMjn'><span id='YkMjn'><b id='YkMjn'><form id='YkMjn'><ins id='YkMjn'></ins><ul id='YkMjn'></ul><sub id='YkMjn'></sub></form><legend id='YkMjn'></legend><bdo id='YkMjn'><pre id='YkMjn'><center id='YkMjn'></center></pre></bdo></b><th id='YkMjn'></th></span></q></dt></tr></i><div id='YkMjn'><tfoot id='YkMjn'></tfoot><dl id='YkMjn'><fieldset id='YkMjn'></fieldset></dl></div>
                  <bdo id='YkMjn'></bdo><ul id='YkMjn'></ul>
                  本文介绍了捕获“堆栈溢出"递归 C++ 函数中的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  是否可以在递归 C++ 函数中捕获堆栈溢出异常?如果是,怎么办?

                  Is it possible to catch a stack overflow exception in a recursive C++ function? If so, how?

                  那么在这种情况下会发生什么

                  so what will happen in this case

                  void doWork()
                  {
                  
                       try() {
                  
                       doWork();    
                       }
                  
                  
                       catch( ... )  {
                  
                       doWork();    
                       }
                  }  
                  

                  我不是在寻找特定操作系统的答案.只是一般

                  I am not looking for an answer to specific OS. Just in general

                  推荐答案

                  这本身并不是一个例外,但如果您只想将堆栈使用限制在一个固定的数量,您可以这样做:

                  It's not an exception per se, but if you just want to be able to limit your stack usage to a fixed amount, you could do something like this:

                  #include <stdio.h>
                  
                  // These will be set at the top of main()
                  static char * _topOfStack;
                  static int _maxAllowedStackUsage;
                  
                  int GetCurrentStackSize()
                  {
                     char localVar;
                     int curStackSize = (&localVar)-_topOfStack;
                     if (curStackSize < 0) curStackSize = -curStackSize;  // in case the stack is growing down
                     return curStackSize;
                  }
                  
                  void MyRecursiveFunction()
                  {
                     int curStackSize = GetCurrentStackSize();
                     printf("MyRecursiveFunction:  curStackSize=%i
                  ", curStackSize);
                  
                     if (curStackSize < _maxAllowedStackUsage) MyRecursiveFunction();
                     else
                     {
                        printf("    Can't recurse any more, the stack is too big!
                  ");
                     }
                  }
                  
                  int main(int, char **)
                  {
                     char topOfStack;
                     _topOfStack = &topOfStack;
                     _maxAllowedStackUsage = 4096;  // or whatever amount you feel comfortable allowing
                  
                     MyRecursiveFunction();
                     return 0;
                  }
                  

                  这篇关于捕获“堆栈溢出"递归 C++ 函数中的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何在 C++ 中创建多个向量的组合而无需硬编码循 下一篇:lambda 函数可以递归吗?

                  相关文章

                  最新文章

                      <legend id='kmLdD'><style id='kmLdD'><dir id='kmLdD'><q id='kmLdD'></q></dir></style></legend>
                    1. <small id='kmLdD'></small><noframes id='kmLdD'>

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