Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Local, automatic variables assume unexpected values if they are read before they are initialized. The C++ Standard, [dcl.init], paragraph 12 [ISO/IEC 14882-2014], states: 

If no initializer is specified for an object, the object is default-initialized. When storage for an object with automatic or dynamic storage duration is obtained, the object has an indeterminate value, and if no initialization is performed for the object, that object retains an indeterminate value until that value is replaced. If an indeterminate value is produced by an evaluation, the behavior is undefined except in the following cases:

— If an indeterminate value of unsigned narrow character type is produced by the evaluation of:
    — the second or third operand of a conditional expression,
    — the right operand of a comma expression,
    — the operand of a cast or conversion to an unsigned narrow character type, or
    — a discarded-value expression,
then the result of the operation is an indeterminate value.
— If an indeterminate value of unsigned narrow character type is produced by the evaluation of the right operand of a simple assignment operator whose first operand is an lvalue of unsigned narrow character type, an indeterminate value replaces the value of the object referred to by the left operand.
— If an indeterminate value of unsigned narrow character type is produced by the evaluation of the initialization expression when initializing an object of unsigned narrow character type, that object is initialized to an indeterminate value.

...

Objects of static or thread storage duration are zero-initialized before any other initialization takes place [ISO/IEC 14882-2014] and need not be explicitly initialized before having their value read.

...

Bibliography

[ISO/IEC 14882-2014]Clause 5, "Expressions"
Subclause 5.3.4, "New"
Subclause 8.5, "Initializers"
Subclause 12.6.2, "Initializing Bases and Members" 
[Lockheed Martin 05]Rule 142, All variables shall be initialized before use

...