1

I know that local variables in C & C++ aren't automatically initialized & if no initial value is given to them they have garbage values. I also know that global & static variables are by default initialized to zero. Use of uninitialized variable results in undefined behavior in C++.

But recently I tried following simple C++ program on various implementations of C++ & all gives me 0 as output.

#include <iostream>
int main()
{
    int u;
    std::cout<<u<<'\n';
}

Output:

CodeBlocks 13.12 IDE: 0

Orwell Dev C++ 5.8.3: 0

Ideone.com: 0 (visit this: http://ideone.com/zWrgwo)

Is it true that modern compilers automatically initializes local variables to 0? or such type of program still represents undefined behavior ? Is it guaranteed to see 0 as output on every modern implementation of C++ always?

Destructor
  • 14,123
  • 11
  • 61
  • 126
  • 3
    [Using in indeterminate value is undefined behavior in C++](http://stackoverflow.com/q/23415661/1708801) in [C it is slightly more nuanced but basically still undefined behavior](http://blog.frama-c.com/index.php?post/2013/03/13/indeterminate-undefined) – Shafik Yaghmour Jun 02 '15 at 13:08
  • Possible duplicate of [Variable initialization in C++](http://stackoverflow.com/q/2218254/1708801) – Shafik Yaghmour Jun 02 '15 at 13:56

3 Answers3

3

No it is not true. Initialising variables to zero would incur an unnecessary performance cost. Non-static variables are not initialised to zero.

The behaviour of using an uninitialised variable in C++ is undefined.

You might find that a debug configuration zero-initialises variables. I find that particularly pernicious and always ensure that any compiler options doing that are switched off.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
3

Specific implementations are free to not set them ("local" variables) to anything, or to set them to any value they want, including zero. The standard doesn't mandate they they be set to a non-zero value, after all :-)

The standard merely states that their value, if not explicitly set, is undefined. Hence it's not something a good programmer would rely upon. This is covered in C++11 8.5 Initializers [dcl.init] /11 (my emphasis):

If no initializer is specified for an object, the object is default-initialized; if no initialization is performed, an object with automatic or dynamic storage duration has indeterminate value.

So, in answer to your closing question, is it guaranteed to see 0 as output on every modern implementation of C++ always?, the answer is no, plain and simple.

As an aside, you may well find that the three "separate" environments you tested on, CodeBlocks, DevC++ and ideone, are all using the same compiler under the covers, so it would hardly be an exhaustive test in that case.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
0

Recent Windows versions (and maybe other OS too) initialize all uninitialized memory to 0 when a program is starting.

Therefore all "uninitialized" variables will appear as 0.

However this depends on the OS, not on the compiler. When running such a program under Windows 9x (which did not have this feature) the variables have random values.

Variables on the stack will contain random values as soon as the stack has been used:

void test()
{
    int a;
    std::cout << a;
}

int main()
{
    someLargeFunction();
    test();
}

The value of the variable "a" is now depending on the function "someLargeFunction"!

Martin Rosenau
  • 17,897
  • 3
  • 19
  • 38
  • I'm slightly unhappy with the term "random" in this context. It's not easy to create truly random values on a deterministic machine. The values are *undefined* from the standpoint of C++ which is OS, run time and compiler agnostic. They are most likely quite specific though, as you mention.-- It's also noteworthy that *accessing* an unitialized variable constitutes UB, strictly spoken (because it could contain a trap value or trigger an "uninitialized" exception in a register, on architectures which support that). – Peter - Reinstate Monica Jun 02 '15 at 14:17
  • Indeed the value will only appear like a random value and not be random in any case. However the value may even be a real random number - for example if timer values or sound card inputs (the lowest bit of such data is said to be really random) is stored in this memory location. – Martin Rosenau Jun 02 '15 at 18:17