gpt4 book ai didi

java - 由于堆栈溢出,C 中通常会发生什么?

转载 作者:搜寻专家 更新时间:2023-11-01 04:02:38 24 4
gpt4 key购买 nike

在 Java 中会有一个堆栈跟踪显示 StackOverflowError 并且整个系统不会崩溃,只有程序会崩溃。

在 C 中,我知道数组索引越界会产生段错误。 C中的堆栈溢出是否也一样,也会出现段错误,即类似问题的相同错误类型?

我不会在 C 语言中测试有意识的无限恢复以查看会发生什么,因为我不知道后果。

或者有时情况会更糟,C 中的堆栈溢出会导致操作系统故障并迫使您重启电源才能恢复?或者更糟的是,造成不可逆转的硬件损坏?堆栈溢出错误会造成多大的影响?

Java 中的保护似乎比 C 中的更好。C 中的保护是否比汇编/机器代码中的保护更好,或者 C 中的保护实际上与程序集相同(缺少)?

最佳答案

In C I'm aware that an array index out of bounds will produce a segmentation fault. Is it the same for a stack overflow in C and there will also be a segmentation fault i.e. same error type for a similar problem?

在 C 中不能保证 会出现段错误。 C 标准说它是 undefined behaviour就这样吧。这可能如何体现,完全取决于实现/平台。

Or is it sometimes something much worse and a stack overflow in C could cause an operating system failure and force you to power cycle to get back? Or even worse, cause irreversible hardware damage? How bad effects can a stack overflow mistake have?

在现代操作系统上,系统很少会发生任何不幸的事情;通常,只有程序会崩溃。现代操作系统使用 various memory protection techniques .

It seems clear that the protection is better in Java than in C. Is it any better in C than in assembly / machine code or is it practically the same (lack of) protection in C as a assembly?

那是因为在 Java 中,内存是“托管的”。在 C 中,它留给程序员;这是设计的。 C 编译器最终会生成机器代码;所以它不会更好或更坏。明显地,一个好的编译器可以检测到其中的一些问题并警告您,与汇编相比,这是 C 语言的优势。

关于java - 由于堆栈溢出,C 中通常会发生什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41634610/

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