If I don't initialize a C++ variable, I find that it automatically gives it 0 on some android phones, but not on others, and zero on all IOS phones. So what determines whether you give it 0, is it the phone system, is it the compiler, is it some compiler option
-
3https://en.cppreference.com/w/cpp/language/initialization – perivesta Sep 16 '20 at 13:08
-
2"The value in an uninitialized variable can be anything – it is unpredictable...": https://en.cppreference.com/book/uninitialized – vahancho Sep 16 '20 at 13:08
-
1`0` is just as good as an uninitialized value as any other integer. Just because the value "looks good" doesn't mean it was set by the compiler. – PaulMcKenzie Sep 16 '20 at 13:16
-
Your OS can give your application pages that are zero filled for security purposes (not to leak information from the previous program that used the same RAM). However this is not really initialization. If your application reuses the same memory the values will not be reset. – drescherjm Sep 16 '20 at 13:19
-
1The value of an uninitialised variable is indeterminate, and accessing its value gives undefined behaviour according to the standard. Practically, the value depends on the value of bytes at the memory location (or machine register) used to store the variable. On some systems (or with some compilers) the value (and therefore behaviour on accessing the value) is due to whatever previously occupied that location (e.g. other variables that no longer exist). On some other systems, the OS or possibly library functions set memory to zero. Some OS deliberately randomise content of memory. – Peter Sep 16 '20 at 15:24
3 Answers
It is undefined behaviour to read an uninitialized variable. If you do that your program is meaningless and the compiler is no longer required to generate anything sensible for the entire program.

- 30,449
- 3
- 47
- 70
-
1
-
4@eerorika This is C++. There are *always* exceptions to every rule ;) But if you ignore those exceptions and just consider reading uninitialized variables as always UB, then you'll be happier and have fewer bugs in the long run. – Jesper Juhl Sep 16 '20 at 13:21
-
@eerorika UB exists at a level of the C++ Standard. Once an implementation generates some machine code, that machine code exactly defines what the program will do. Just the Standard doesn't care anymore. – Daniel Langr Sep 16 '20 at 13:38
-
@DanielLangr There can be UB in machine code level as well, and not all instruction sets necessarily define what the program does in all cases. But that is not the UB that the C++ standard describes. If C++ program has UB, then the resulting program could be non-deterministic, or it might not be. Neither is guaranteed. – eerorika Sep 16 '20 at 13:45
-
@eerorika Any example of UB at a machine/assembly code level? Just curious... – Daniel Langr Sep 16 '20 at 14:51
-
@DanielLangr See for example: https://en.wikipedia.org/wiki/Illegal_opcode – eerorika Sep 16 '20 at 15:11
-
@eerorika Thanks. It seems to be a case of old CPUs: _More recent CPUs, such as the 80186, 80286, 68000 and its descendants, do not have illegal opcodes that are widely known/used. Their manufacturers guarantee that the CPU will behave in a well-defined way when it finds an unknown opcode in the instruction stream; Usually this means triggering a certain exception or fault condition._ – Daniel Langr Sep 16 '20 at 15:14
-
1@DanielLangr Read further: *In spite of this manufacturer guarantee against such instructions, research using techniques such as fuzzing has uncovered a vast number of undocumented instructions in modern x86 processors.* I recommend this talk: https://www.youtube.com/watch?v=KrksBdWcZgQ – eerorika Sep 16 '20 at 15:15
-
Uninitialized variables will have whatever value was in the memory cell; from other programs, maybe from startup. I have seen memory chips that boot up to all 1 bits and others that boot up all zero bits. – Thomas Matthews Sep 16 '20 at 15:42
-
@ThomasMatthews "Uninitialized variables will have whatever value was in the memory cell; from other programs" - That's assuming that the compiler didn't decide to use the fact that your program invoked UB to just optimize the whole thing to `return 0;` from `main` and nothing else. Which it is absolutely allowed to do. – Jesper Juhl Sep 16 '20 at 16:21
What determines the initial value of C++ variables
If I don't initialize a C++ variable
I assume you refer to automatic storage. Variables in static or thread local storage are zero initialised implicitly.
From perspective of the language: The initial value is simply indeterminate. Whether anything affects this value indirectly is unspecified. The behaviour of reading an indetrminate value is in most cases undefined.
In practice: It is typically determined by whatever happens to be in the memory before it was allocated for the variable.
is it some compiler option
Perhaps, or maybe not. It's impossible to tell without seeing the compiler and options that were used.
I know that people have experimented with a compiler feature that initialises uninitialised variables, but I don't know of an official mainstream compiler release with such option. That doesn't mean it doesn't exist.

- 232,697
- 12
- 197
- 326
C++ variables are not warrantied to be initialized unless you define a constructor for the class of them.
In case of basic types or structs of basic types (aka POD, Plain Old Data) the value will be undefined unless you explicitly assign something to them.
Some compiler initialize them, some do it when you are generating debug information, but it is not a portable nor trusted way to do it.
You should not use uninitialized variables, as such will produce an undefined behavior.

- 1,061
- 5
- 14