读取未初始化的变量

Reading uninitialized variable

读取未初始化的变量会导致未定义的行为,例如

#include <iostream>

int main()
{
    int a;
    std::cout << a << std::endl; // undefined behavior
}

有人可以对这个事实给出正式的解释吗?

这里是相关的部分,我认为:

4.1 Lvalue-to-rvalue conversion

1 - A glvalue of a non-function, non-array type T can be converted to a prvalue. If T is an incomplete type, a program that necessitates this conversion is ill-formed. If the object to which the glvalue refers is not an object of type T and is not an object of a type derived from T, or if the object is uninitialized, a program that necessitates this conversion has undefined behavior.

变量是左值,我认为“左值到右值的转换”是获取变量值的过程。

(注意 - 我不熟悉 C++ 标准,所以我可能没有找到适用于此示例的部分。我所做的只是在 PDF 中搜索“未初始化”,然后查找匹配项看起来最相关。)