读取未初始化的变量 [英] Reading uninitialized variable

查看:103
本文介绍了读取未初始化的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Reading uninitialized variables leads to undefined behavior, e.g.

#include <iostream>

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

有人可以对此事实进行正式解释吗?

Can someone give a formal explanation of this fact?

推荐答案

我认为这是相关部分:

4.1左值到右值转换

4.1 Lvalue-to-rvalue conversion

1-非函数,非数组类型T的glvalue可以转换为prvalue.如果T不完整 类型,则表示需要进行此转换的程序的格式不正确.如果 glvalue所引用的对象不是类型T的对象,并且 不是从T,派生的类型的对象,或者如果该对象是 未初始化,需要进行此转换的程序具有 未定义的行为.

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.

变量是左值,我认为从左值到右值的转换"是获取变量值的过程.

A variable is an lvalue, and I think an "lvalue to rvalue conversion" is the process of taking a variable's value.

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

(Note - I'm not familiar with the C++ standards, so I may have not found the part that applies to this example. All I did was search the PDF for "uninitialized", and look for the hit that looks most relevant.)

这篇关于读取未初始化的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆