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**
]
这应该总是段错!?
不一定,未定义的行为意味着任何事情都可能发生.