捕捉“堆栈溢出”在递归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?
这样会发生什么
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屋!