tags:

views:

73

answers:

2

Hi!

I learned that auto-variables aren't initialized to zero. So the following code will behave correctly and prints random numbers on the screen:

#include <iostream>
using std::cout;
using std::endl;

void doSomething()
{
  int i;
  cout << i << endl;
}

int main()
{
  doSomething();
}

But why won't this snipped behave in the same way ?

#include <iostream>
using std::cout;
using std::endl;

void doSomething()
{
  int i;
  cout << i << endl;
}

int main()
{
  int j;
  cout << j << endl;
  doSomething();
}

This snippet shows:

0
0

Does anyone know why "j" and "i" were suddenly initialized to zero here?

+4  A: 

Using an uninitialized variable is undefined behaviour. It's likely you wouldn't get the same behaviour on another compiler. The only 'why' is to be found inside the source code to your specific C++ compiler.

Pontus Gagge
that's true! I tried it out on a different machine and the code behaved correctly.
Mike Dooley
well, it is an incorrect answer though, Stroustrup is in accordance with section 8.5 of the C++ standard (ISO-IEC 14882-2003)
ufotds
Actually, the "why" often has little to do with the compiler. The value will typically be whatever was left there by whatever executed previously, or whatever the OS put there if the memory is newly allocated (the latter will often be zero for security reasons).
Jerry Coffin
@Mike: The function will behave "correctly" even if it prints out `0` every time. You cannot rely on the number being "random".
Bill
@ufotds: Huh? Section 8.5 discusses declarators initializers. The cited code has no initializers. Ergo undefined behaviour, unless the Standard is even more arcane than I thought! Which can never be ruled out...@Mike: @Bill is right. Your code has no correct behaviour. @Jerry: of course the O/S matters - but I'd say the exact code the compiler emits is pretty much **the** factor to check first. Take compiler flags to always zero out or 0xDEADBEEF storage, f'rinstance.
Pontus Gagge
@Pontus: In my version of that standard (which is most definitely arcane) 8.5.6 reads: "Every object of static storage duration shall be zero-initialized at program startup before any other initializationtakes place." In my opinion, that is not *undefined behavior*, especially since 8.5.5 goes into detail about what zero initializing is.
ufotds
@ufotds: however, static storage is clearly not involved in this question, so the section isn't terribly relevant...
Pontus Gagge
A: 

If an initializer is specified for an object, that initializer determines the initial value of an object. If no initializer is specified, a global (§4.9.4), namespace (§8.2), or local static object (§7.1.2, §10.2.4) (collectively called static objects) is initialized to 0 of the appropriate type.

For example:

// Global scope

int    a; // means ‘‘int    a = 0  ;’’
double d; // means ‘‘double d = 0.0;’’

Local variables (sometimes called automatic objects) and objects created on the free store (sometimes called dynamic objects or heap objects) are not initialized by default.

For example:

void f()
{
   int x; // x does not have a well defined value
   // ...
}

From Stroustrup, The C++ Programming Language, 3rd edition, p.83 - §4.9.5

ufotds
Er... I don't know if you're jocking, but it's exactly the opposite...---EDIT---Ok, you edited and now it's correct.
Matteo Italia
sorry, my memory is a sieve sometimes, corrected now. I know, it took me the full 2 minutes to look it up.
ufotds
OK, that's what you were after. I' taking the liberty of adding a '// Global scope' comment to your first example, them.
Pontus Gagge