为什么我没有拿到"分段错误"? [英] Why don't i get "Segmentation Fault"?
问题描述
可能重复:结果
<一href=\"http://stackoverflow.com/questions/12410016/why-dont-i-get-a-segmentation-fault-when-i-write-beyond-the-end-of-an-array\">Why不,我得到一个分段错误,当我写超出数组的结束?
块引用>这code编译并没有任何错误运行。但如何?
的#include&LT;&stdio.h中GT;INT主要(无效)
{
INT富[2]; 富[8] = 4; / *这怎么可能发生? * / 的printf(%d个\\ N,富[8]); 返回0;
}我是上的Arch Linux x86_64的GCC 4.7.2 编译。
的gcc -o -Wall主的main.c
解决方案由于不确定的行为并不意味着您将收到一个段错误,这将被定义的行为。
让我们假设你在调试模式下运行,你的编译器是填充你的筹码/局部变量的空间。你可能只是写入堆栈空间的一些未使用的部分。
建立在周一发布版本时,你的编译器是感觉心情烦躁,现在你覆盖返回地址或code,用于建立呼叫
的printf
, 随你。哎呀。只是一个可能的结果,但你的想法。
Possible Duplicate:
Why don’t I get a segmentation fault when I write beyond the end of an array?This code compiles and runs without any error. But how?
#include <stdio.h> int main (void) { int foo[2]; foo[8] = 4; /* How could this happen? */ printf("%d\n", foo[8]); return 0; }
I'm compiling with GCC 4.7.2 on Arch Linux x86_64.
gcc -Wall -o "main" "main.c"
解决方案Because undefined behavior doesn't mean "you will receive a segfault", that would be defined behavior.
Let's assume you're running in debug mode and your compiler is padding your stack/local variable space. You're probably just writing into some unused part of the stack space.
Build a release version on a Monday when your compiler is feeling cranky and now you overwrite the return address, or the code that sets up the call to
printf
, whatever. Oops.Just one possible outcome, but you get the idea.
这篇关于为什么我没有拿到&QUOT;分段错误&QUOT;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!