Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

In C/C++, why are globals and static variables initialized to default values?

Why not leave it with just garbage values? Are there any special reasons for this?

like image 465
Xinus Avatar asked Jan 19 '10 06:01

Xinus


People also ask

Why do we need to initialize static variables?

When static keyword is used, variable or data members or functions can not be modified again. It is allocated for the lifetime of program. Static functions can be called directly by using class name.

Why are global variables always initialized to zero?

Global and static variables are initialized to their default values because it is in the C or C++ standards and it is free to assign a value by zero at compile time. Both static and global variable behave same to the generated object code.

Is static variable initialized by default?

If you declare a static variable in a class, if you haven't initialized them, just like with instance variables compiler initializes these with default values in the default constructor.

Why are static variables initialized to zero?

Yes, it's because it's in the standard; but really, it's because it's free. Static variables look just like global variables to the generated object code.


2 Answers

  1. Security: leaving memory alone would leak information from other processes or the kernel.

  2. Efficiency: the values are useless until initialized to something, and it's more efficient to zero them in a block with unrolled loops. The OS can even zero freelist pages when the system is otherwise idle, rather than when some client or user is waiting for the program to start.

  3. Reproducibility: leaving the values alone would make program behavior non-repeatable, making bugs really hard to find.

  4. Elegance: it's cleaner if programs can start from 0 without having to clutter the code with default initializers.

One might then wonder why the auto storage class does start as garbage. The answer is two-fold:

  1. It doesn't, in a sense. The very first stack frame page at each level (i.e., every new page added to the stack) does receive zero values. The "garbage", or "uninitialized" values that subsequent function instances at the same stack level see are really the previous values left by other method instances of your own program and its library.

  2. There might be a quadratic (or whatever) runtime performance penalty associated with initializing auto (function locals) to anything. A function might not use any or all of a large array, say, on any given call, and it could be invoked thousands or millions of times. The initialization of statics and globals, OTOH, only needs to happen once.

like image 65
DigitalRoss Avatar answered Sep 21 '22 05:09

DigitalRoss


Because with the proper cooperation of the OS, 0 initializing statics and globals can be implemented with no runtime overhead.

like image 36
R Samuel Klatchko Avatar answered Sep 22 '22 05:09

R Samuel Klatchko