为什么不这个段落错误

And*_*ira 4 c c++ printf segmentation-fault

我偶然发现了一些"有趣"的东西,我无法理解为什么行为不连贯.

检查此代码.

char buf[100];
sprint(buf,"%s",bla);
Run Code Online (Sandbox Code Playgroud)

简单,对.这很容易理解什么时候要去bla是一个NULL指针.

这应该总是段错!?

在一台机器上,可执行段错误,在另一台机器上(我的开发机器),它就像往常一样.

我的开发PC正在运行Windows7,我正在编译gcc/MingW.崩溃的计算机XP已经Visual studio 6安装完毕.

为什么我的电脑没有崩溃?

Pra*_*rav 24

ISO C99: 7.19.6.3 The printf function

概要

 #include <stdio.h>
 int printf(const char * restrict format, ...);
Run Code Online (Sandbox Code Playgroud)

The printf function is equivalent to fprintf with the argument stdout interposed before the arguments to printf.

7.19.6.1 The fprintf function

7.19.6.1.9

If a conversion speci?cation is invalid, the behavior is **unde?ned**. If any argument is not the correct type for the corresponding conversion speci?cation, the behavior is **unde?ned**.

所以你的代码调用Undefined Behavior [ (ISO C99 3.4.3) behavior, upon use of a nonportable or erroneous program construct or of erroneous data, for which this International Standard imposes **no requirements**]

这应该总是段错!?

不一定,未定义的行为意味着任何事情都可能发生.

  • 有一天,我将制作一个编译器,每次调用未定义的行为时,都会开始播放*Valkyrie*的飞行. (17认同)
  • 很好地研究,Prasoon,`+ 1`来自我! (6认同)

sbi*_*sbi 11

这应该总是段错!?

不会.这会调用未定义的行为.分段错误只是调用UB的许多可能结果之一.

  • +1出于某种原因,未定义的行为称为"未定义". (4认同)