读取未初始化的变量

编程入门 行业动态 更新时间:2024-10-17 11:30:01
本文介绍了读取未初始化的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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.)

更多推荐

读取未初始化的变量

本文发布于:2023-07-28 02:31:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1226476.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:初始化   变量

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!