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

    1. <small id='uBGD2'></small><noframes id='uBGD2'>

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

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

        时间:2024-08-13

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

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

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

                  <tfoot id='z2ofE'></tfoot>
                  本文介绍了捕捉“堆栈溢出"递归 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++ 函数中的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:新 (std::nothrow) 与 try/catch 块中的新 下一篇:Qt设置QLineEdit的背景颜色

                  相关文章

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

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

                  <tfoot id='dTeYL'></tfoot>
                  1. <legend id='dTeYL'><style id='dTeYL'><dir id='dTeYL'><q id='dTeYL'></q></dir></style></legend>
                      <bdo id='dTeYL'></bdo><ul id='dTeYL'></ul>