variable default values

This is a discussion on variable default values within the C++ Programming forums, part of the General Programming Boards category; WTF I must say, I'm just learning C++ and I have to declare my variables with a value, ie
int ...

Because it only creates a variable and hold whatever junk was where that variable now is. Why would it default to 0 ? It's not even wish able to always default to 0. Why would you even use a variable without assigning it a value in the first place ? That's why your app crashed...

No it's not a compiler bug, it is normal behaviour.
Built-in types don't get initialised by default, probably for speed reasons. Quite often the value will be set in subsequent code before it is used, and the compiler might have trouble figuring out that it doesn't need to initialise to zero. Hence the language is designed so that you have to be explicit.

Code:

//use:
int x = 0;
//or
int x(0);

Desolation: Some languages do default global variables (at least) to zero, so his question is quite understandable.

I'm guessing that the reason static and global variables get initialized to 0 by default, and automatic variables don't, is that the former only need to be initialized once in the life of the program, while the latter could need the initialization an arbitrarily large number of times, so doing it by default could be very wasteful. Is this correct?

most compilers will try to place uninitialized global variables in the BSS segment.

In an object module compiled from C, the bss section contains the local variables (but not functions) that were declared with the static keyword, except for those with non-zero initial values. (In C, static variables are initialized to zero by default.) It also contains the non-local (both extern and static) variables that are also initialized to zero (either explicitly or by default).

Yes, even C++ should initialize GLOBAL (or static local) variables to zero (either by setting them to zero in DATA section or just leaving it to the loader to get it done when initializing the BSS section). Otherwise, old C code can't be compiled in a C++ environment.

Local variables, however, are not defined to have any particular value - whatever happens to be the value at the location of that variable. As stated above, this is for performance reasons, since a value of zero is not necessarily the value wanted by the programmer, setting all local variables to zero, only for them to be set to something else later, is pretty pointless.

[Debug mode of MSVC will actually set local variables to a decidedly non-zero value that is almost certain to be an invalid pointer and too large an index (I beleive it fills local storage with 0xCC bytes). This is so that it's easier to detect if the variable ISN'T set to a value]

Also, gcc will only detect unset variables at certain level of optimization, because it's part of "flow analyzis" (to say if a variable is set or not, you obviously need to analyze the flow of the code, unless it's a very trivial case - which is usually not the ones that need help), which is not run at the no optimization level.

Thanks for all the information. A friend pointed out that this information is in the beginning of the book we are studying. But since I read the first chapter a year ago (mostly pointless fluff) I didn't re-read it again. Oops. big mistake.