堆栈溢出和分段错误

发布于 2024-11-30 11:08:12 字数 188 浏览 2 评论 0原文

在这种情况下,当无限递归运行时,堆栈溢出是否会导致分段错误。我期待着像“Stack Overflow”这样的错误消息!

#include <stdio.h>
int main(){
static int a=1;
printf("%d\n",a);
a++;
main();
return 0;
}

Does Stack overflow gives segmentation fault in this case, when this infinite recursion runs. I was expecting an error message like "Stack Overflow"!

#include <stdio.h>
int main(){
static int a=1;
printf("%d\n",a);
a++;
main();
return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

自此以后,行同陌路 2024-12-07 11:08:12

它将导致分段错误,因为堆栈会溢出。

所发生的情况是,每次调用 main 都会将更多数据推送到堆栈上,以便您的程序知道从 main() 返回后跳转到哪里。最终,您将耗尽堆栈空间(堆栈溢出)。此时,您对 main 的下一次调用将尝试将数据推送到堆栈。由于没有更多可用的堆栈空间,它会意外写入无效的内存位置,从而触发分段错误。这与您在数组末尾写入内容类似。

It will cause a segmentation fault because the stack will overflow.

What happens is that each call to main pushes some more data onto the stack so that your program knows where to jump once it returns from main(). Eventually, you will run out of stack space (a stack overflow). At this point, your next call to main will try to push data to the stack. Since there is no more stack space available, it will accidentally write to an invalid memory location thus triggering the segmentation fault. This is similar to when you write past the end of an array.

吻安 2024-12-07 11:08:12

堆栈溢出是一个原因;堆栈溢出的影响(在许多情况下)是分段错误。创建错误消息的代码是否能够从结果中推断出原因是堆栈溢出,这取决于操作系统、体系结构和运行时环境。但大多数人都懒得去尝试。

A stack overflow is a cause; the effect of the stack overflow is (in many cases) a segmentation fault. It depends on the OS, architecture and runtime environment whether the code the creates the error message will be able to deduce from the effect that the cause was a stack overflow. But most don't bother to try.

ぇ气 2024-12-07 11:08:12

正如其他人之前发布的那样,这取决于环境(操作系统、硬件、编译器等)。根据这一点,错误消息可能不会反映实际原因(通常在进行无效内存访问时)。也就是说,在 valgrind 之类的东西中运行它应该始终为您提供有关此类访问原因的可靠信息。

As others before posted, it depends on the environment (os, hardware, compiler, etc.). Depending on this, the error message might not reflect the actual cause (as often when doing invalid memory access). That said, running it in something like valgrind should always give you reliable information on the cause of such access.

半枫 2024-12-07 11:08:12

你只是做了一个 int 溢出。

达到最大值后,它会继续下降到负值。

这个递归会破坏你的调用堆栈!

void lol()
{
lol();
}

这是缓冲区溢出的情况:
https://www.owasp.org/index.php/Buffer_Overflow

这不是编程故障但存在一些安全问题。

就像你没有验证密码输入 len 一样,如果 len 像 char[6] 那样声明,那家伙就会向你发送 4000000 个字符,这将溢出你的 char* 。

You're only doing a int overflow.

After reaching his maximum value it go on his minus value.

This recursion will smash you call stack!

void lol()
{
lol();
}

Here it is for buffer overflow:
https://www.owasp.org/index.php/Buffer_Overflow

It's not a programming fault but some security issue.

Like you don't verify the password input len, and the guy send you 4000000 chars that will overflow your char* if len declared like char[6].

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文