[英]How can I get a stack overflow in C++ and Unix?
I'm wondering how to get an stack overflow error with a simple example, such as: 我想知道如何通过一个简单的例子来获得堆栈溢出错误,例如:
int recursSum (int n)
{
return (n==1)? 1:n+recursSum(n-1);
}
I ask that stupid question because I only have some Segmentation fault , even with an empty function calling itself… 我问这个愚蠢的问题,因为我只有一些Segmentation错误 ,即使有一个空函数调用自己...
Am I missing something or is there any protection or something that prevents me for doing this? 我是否遗漏了某些东西,或者是否有任何保护或阻止我这样做的事情?
A segmentation fault means that the memory protection kicked in and prevented you from accessing memory you did not have available. 分段错误意味着内存保护启动并阻止您访问您没有的内存。 This can occur for a variety of reasons , but one reason indicated is stack overflow (overflowing the stack into some other segment of memory).
这可能由于各种原因而发生,但是指出的一个原因是堆栈溢出(将堆栈溢出到其他一段内存中)。
If the function is called with a negative or 0 integer argument, you'll face infinite recursion. 如果使用负数或0整数参数调用函数,则将面临无限递归。 However, the compiler likely can tail call optimize that particular function and you'd never see a stack overflow except in debug mode.
但是,编译器可能会尾部调用优化该特定函数,除了在调试模式下,您永远不会看到堆栈溢出。 The segmentation fault lies somewhere else.
分段错误位于其他地方。
A stack overflow is a type of segmentation fault, it looks like your system has just output a generic error. 堆栈溢出是一种分段错误,看起来您的系统只输出一般错误。
You can read more here: http://en.wikipedia.org/wiki/Segmentation_fault 您可以在这里阅读更多内容: http : //en.wikipedia.org/wiki/Segmentation_fault
Foo()
{
float f[1024];
Foo();
}
f is a dummy variable which will help filling stack quickly. f是一个虚拟变量,可以帮助快速填充堆栈。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.