Why is an uninitialized variable being printed in C ++?

Why does this print 32767

(or some other random number)? What is std::cout

printing? Why is it not NULL

(or 0

)?

int main() 
{
    int a;
    std::cout << a;
}

      

+3


source to share


4 answers


This is because variables with automatic storage duration are not automatically initialized in C ++. In C ++, you don't pay for what you don't need, and the automatic initialization of a variable takes time (setting zero to a memory location ultimately comes down to machine (its) construction, which is then translated into electrical signals that drive the physical bits) ...

This variable is reserved for memory location, and it happens that some unwanted file is located in this memory location. This stuff is printed cout

.

As @dwcanillas pointed out, this behavior is undefined. Related: What happens to a declared, uninitialized variable in C? Does it matter?



From the C ++ standard (underline mine):

8.5 Initializers [dcl.init]

7) By default, to initialize an object of type T means :

  •   
  • If T is a class of a class (possibly cv-qualit) (clause 9), constructors count. The corresponding constructors are listed (13.3.1.3), and the best one for the initializer () is chosen through overload resolution (13.3). the constructor chosen this way is called with an empty argument list to initialize -> the object.  
  • If T is an array type, each element is initialized by default.  
  • Otherwise, initialization fails.  

12) If no initializer is specified for the object, the object is initialized by default . When storage for an object with automatic or dynamic storage duration is obtained, the object has an undefined value, and if the object is not initialized, that object retains the undefined value until that value is replaced (5.18). [Note. Objects with static or duration of storing streams are zero-initialized, see 3.6.2. - end note] If an undefined value is generated by an evaluation, the behavior is undefined , except in the following cases:

- If an unsigned unsigned narrow character value (3.9.1) is obtained by evaluating:

     

- the second or third operand of the conditional expression (5.16),

     

- right operand with comma (5.19),

     

- casting operand or conversion to unsigned narrow character type (4.7, 5.2.3, 5.2.9, 5.4) or

     

- expression with dropped value (section 5)

     

...

+14


source


This behavior is undefined. You print what takes up memory a

, which is what happens in this case 32767

.



+6


source


The behavior extends to C ++ 14 (N3936) [dcl.init] / 12:

If no initializer is specified for an object, the object is initialized by default. When storage is obtained for an object with automatic or dynamic storage duration, the object has an undefined value, and if the object is not initialized, the object retains the undefined value until that value is replaced.

[...] If an undefined value is generated by an evaluation, the behavior is undefined , except in the following cases:

and your code does not cover any of the "following cases", which cover several situations in which unsigned char

undefined values ​​are allowed to propagate.

+1


source


Because "a" is not global / static. Its an automatic variable, for which initialization occurs at runtime. If it were global, then zero initialization would occur at compile time. i.e

β€’ static variables are initialized at compile time, since their address is known and fixed. Initializing them to 0 requires no execution time.

β€’ automatic variables can have different addresses for different calls and must be initialized at runtime with each function call, which can result in a runtime need. If you need this initialization, please request it.

0


source







All Articles