捕捉“堆栈溢出”递归C ++函数中的异常 [英] Catching "Stack Overflow" exceptions in recursive C++ functions
问题描述
可以在递归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
推荐答案
实际上没有便携式方式。当控制递归函数尝试在堆栈地址空间之外分配堆栈帧时,通常会导致无效的存储器访问。这通常会根据操作系统崩溃您的程序与分段故障/访问冲突。换句话说,它不会抛出一个可以通过语言以标准方式处理的c ++异常。
There's really no portable way to do it. An out of control recursive function will usually cause an invalid memory access when it tries to allocate a stack frame beyond the stack address space. This will usually just crash your program with a Segmentation Fault/Access Violation depending on the OS. In other words, it won't throw a c++ exception that can be handled in a standard way by the language.
这篇关于捕捉“堆栈溢出”递归C ++函数中的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!