没有得到分段错误使用C [英] Not getting Segmentation Fault in C
本文介绍了没有得到分段错误使用C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这里是C code:
char **s;
s[334]=strdup("test");
printf("%s\n",s[334]);`
我知道的strdup确实的测试的分配,但如果s [334],我们将会把字符串指针测试不分配,然而,这code就像一个魅力
i know that strdup does the allocation of "test", but the case s[334] where we will put the pointer to the string "test" is not allocated,however,this code works like a charm
推荐答案
编译器对我们来说是太聪明了!它知道的printf(%S \\ n,some_string)
是完全一样的看跌期权(some_string)
,因此它可以简化
The compiler is too smart for us! It knows that printf("%s\n", some_string)
is exactly the same as puts(some_string)
, so it can simplify
char **s;
s[334]=strdup("test");
printf("%s\n",s[334]);
到
char **s;
s[334]=strdup("test");
puts(s[334]);
和然后(假设没有UB)即再次相当于
and then (assuming no UB) that is again equivalent to
puts(strdup("test"));
所以,偶然的段故障没有发生(这一次)。
So, by chance the segment fault didn't happen (this time).
这篇关于没有得到分段错误使用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文