Some variables in C + + are automatically assigned a value of 0 by the compiler if they are not assigned, but some variables do not, and a random number is given, which is discussed in detail below:
First look at a few stores in C + +:
1, the Stack area: The compiler automatically allocates the release, the stored function parameter value, the local variable value and so on. It operates in a manner similar to a stack in a data structure.
2, heap area: Generally by the programmer allocation release, if the programmer does not release, the end of the program may be recycled by the OS. Note that it is not the same as the heap in the data structure, the distribution is similar to the list, hehe.
3. Global zone (Static): the storage of global variables and static variables is placed in a block, initialized global variables and static variables in an area, uninitialized global variables and uninitialized static variables in another adjacent area . -System release after the program is finished
4, constant area: the constant string is put here. Released by the system after the program is finished
5, program code area: the binary code that holds the function body.
In the above storage areas, if the variables defined in the global zone are not initialized by the user, the compiler automatically initializes them to 0.
It is important to note that you define two words instead of declarations.
Let's look at the difference between definitions and declarations: From a compilation standpoint, a declaration simply tells the compiler that a variable of a certain type will be used, but the compiler will not allocate any memory for it. And the definition is allocating memory. Since the declaration does not allocate memory, it is naturally impossible for the compiler to automatically initialize to 0.
Conclusion: Some global variables (whether with no static modifiers) or local variables decorated with static are automatically initialized to 0 by the compiler when they are defined, and no variables are automatically initialized by the compiler at the time of declaration. such as static int num, if any position placed in the function is implicitly initialized to 0, but does not have a value if it is written in the declaration of the class.
Problems in automatic initialization of variables in C + +