gpt4 book ai didi

c++ - 为什么在某些机器上堆栈溢出,但在另一台机器上出现段错误?

转载 作者:IT老高 更新时间:2023-10-28 12:40:21 25 4
gpt4 key购买 nike

出于好奇,我正在尝试生成堆栈溢出。此代码生成堆栈溢出 according to the OP ,但是当我在我的机器上运行它时,它会产生段错误:

#include <iostream>

using namespace std;

int num = 11;
unsigned long long int number = 22;

int Divisor()
{
int result;
result = number%num;

if (result == 0 && num < 21)
{
num+1;
Divisor();

if (num == 20 && result == 0)
{
return number;
}
}

else if (result != 0)
{
number++;
Divisor();
}
}

int main ()
{
Divisor();
cout << endl << endl;
system ("PAUSE");
return 0;
}

另外,根据 this帖子,那里的一些例子也应该这样做。为什么我会出现段错误?

最佳答案

Why is it I get segmentation faults instead?

您所看到的段错误是堆栈溢出的副作用原因是堆栈溢出,结果是段错误。

来自 wikipedia article对于“堆栈溢出”(强调我的)

.... When a program attempts to use more space than is available on the call stack (that is, when it attempts to access memory beyond the call stack's bounds, which is essentially a buffer overflow), the stack is said to overflow, typically resulting in a program crash.

关于c++ - 为什么在某些机器上堆栈溢出,但在另一台机器上出现段错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30345067/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com