0

The value -858993460 is being displayed by cout for an uninitialized variable (presumably because there is junk in memory), but it will output -858993460 for every uninitialized variable in the program no matter how many I have or where they are in the program in relation to each other.

It will also output this same value, -858993460, when I run a completely different program with an uninitialized variable on a completely different computer, from Windows 7 to Windows 10 running on a VM on a Mac. Why is it doing this? The likelihood of -858993460 randomly being in each uninitialized memory location on different computers is astronomically unlikely.

resueman
  • 10,572
  • 10
  • 31
  • 45
James
  • 17
  • 1
  • 4
    Undefined behavior is undefined behavior is undefined behavior. Some compilers use special values for uninitialized variables when building in debug, this is why you see the same value for all of them. – Captain Obvlious Sep 09 '15 at 16:45
  • 1
    Compiling with different options might give a different value. It's undefined, so the compiler is free to do *anything* (or nothing) with that space. It may be that it's filling all memory with this value (hex: CCCCCCCC) for debugging purposes. – Paul Roub Sep 09 '15 at 16:45
  • Run in release mode and it will probably be different. – Neil Kirk Sep 09 '15 at 16:47
  • The reason that it's filled that way? `0xCC` is the `INT 3` opcode, aka the debugger break instruction. AFAIK the compiler only does this for debug builds. – theB Sep 09 '15 at 16:50

2 Answers2

9

Reading an uninitialized variable isn't guaranteed to return a random value or even a value that previously existed at that location.

-858993460 is 0xcccccccc. It's the bit pattern that MSVC uses to fill uninitialized values in debug builds. This helps you identify cases of uninitialized variables.

zneak
  • 134,922
  • 42
  • 253
  • 328
8

-858993460 == 0xcccccccc in hex. This bit pattern is often used by microsoft compilers to detect buffer overruns and such.

Akash Rupela
  • 159
  • 10