刨根问底:C++中未初始化全局变量为什么都会被编译器自动置0

来源:互联网 发布:淘宝主图制作教程 编辑:程序博客网 时间:2024/05/21 17:56

有这个疑问很久了,今天搜了一上午资料想知道为什么。我们都知道,C++ 11 ISO标准中,未初始化的全局变量会被置0,原文如下。


Variables with static storage duration (3.7.1) or thread storage duration (3.7.2) shall be zero-initialized (8.5)before any other initialization takes place.

以上摘自C++11 ISO文档从以上这段话不难看出,确切说,全局变量是先被初始化为0,然后才进行其他的初始化。


好,既然标准如此,这么一个规定,到底是个死规定,还是有背后的技术原因?


查阅了很多文档。找到了一些信息,大体摘录一些。


Security: leaving memory alone would leak information from other processes or the kernel.Efficiency: the values are useless until initialized to something, and it's more efficient to zero them in a block with unrolled loops.Reproducibility: leaving the values alone would make program behavior non-repeatable, making bugs really hard to find.Elegance: it's cleaner if programs can start from 0 without having to clutter the code with default initializers.One might wonder why the auto storage class does start as garbage. The answer is two-fold:It doesn't, in a sense. The very first stack frame does receive zero values. The "garbage", or "uninitialized" values that subsequent instances at the same stack level see are really the previous values left by the same program.There might be a runtime performance penalty associated with initializing auto (function locals) to anything. A function might not use any or all of a large array, say, and it could be invoked thousands or millions of times. The initialization of statics and globals, OTOH, only needs to happen once.

原文:Why global and static variables are initialized to their default values?

这段回答说了一些原因,但没一条是技术原因,而且似乎也都不是置零的硬性条件。


Because that's the way it is, according to the C Standard. The reason for that is efficiency:static variables are initialized at compile-time, since their address is known and fixed. Initializing them to 0 does not incur a runtime cost.automatic variables can have different addresses for different calls and would have to be initialized at runtime each time the function is called, incurring a runtime cost that may not be needed. If you do need that initialization, then request it.

原文:why global variables are always initialized to '0', but not local variables?

这个回答和上面的差不多,主要提了效率因素。简单说,全局变量是在编译期初始化的,而局部变量是运行期,原因则是地址会变化。


It is required by the standard (§6.7.8/10).There's no technical reason it would have to be this way, but it's been that way for long enough that the standard committee made it a requirement.Leaving out this requirement would make working with static variables somewhat more difficult in many (most?) cases. In particular, you often have some one-time initialization to do, and need a dependable starting state so you know whether a particular variable has been initialized yet or not. 

原文:Why are static variables auto-initialized to zero?


这个回答很简答直接,但却是最让人信服的,因为C语言一向如此,慢慢就被写入到标准中了。



总结下上面的几个回答,没啥特殊的技术原因,是标准的硬性要求。


本人猜测,若是局部变量的地址也是确定的,那么也会被自动初始化为0,毕竟如果用garbage value初始化变量,程序的行为完全是未知的。


其实说到底,把所有用户未初始化的变量置0才是正道,局部变量不这么做不是因为不想,而是因为条件不允许。



0 0
原创粉丝点击