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 , states [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.

...

In this noncompliant code example, an uninitialized local variable is evaluated as part of an expression to print its value, resulting in undefined behavior:

Code Block
bgColor#FFcccc
langcpp
#include <iostream>
 
void f() {
  int i;
  std::cout << i;
}

...

In this noncompliant code example, an int * object is allocated by a new-expression, but the memory it points to is not initialized. The object's pointer value and the value it points to are printed to the standard output stream. Printing the pointer value is well-defined, but attempting to print the value pointed to yields an indeterminate value, resulting in undefined behavior.

Code Block
bgColor#FFcccc
langcpp
#include <iostream>
 
void f() {
  int *i = new int;
  std::cout << i << ", " << *i;
}

...

When moving a value from an object of standard library type, the moved-from object's value is generally left in a valid but unspecified state. The notable exception to this rule is std::unique_ptr, which is guaranteed to represent a null pointer value when it has been moved from. In this noncompliant code example, the integer values 0 through 9 are expected to be printed to the standard output stream from a std::string rvalue reference. However, because the object is moved and then reused under the assumption its internal state has been cleared, unexpected output may occur despite not triggering undefined behavior.

Code Block
bgColor#FFcccc
langcpp
#include <iostream>
#include <string>

void g(std::string &&v) {
  std::cout << v << std::endl;
}

void f() {
  std::string s;
  for (unsigned i = 0; i < 10; ++i) {
    s.append(1, static_cast<char>('0' + i));
    g(std::move(s));
  }
}

...

Bibliography

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

...