为什么在C ++中打印一个未初始化的变量? [英] Why does an uninitialized variable print in C++?

查看:224
本文介绍了为什么在C ++中打印一个未初始化的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么打印 32767 (或其他随机数字)?什么是 std :: cout 打印?为什么 NULL (或 0 )?

Why does this print 32767 (or some other random number)? What is std::cout printing? Why is it not NULL (or 0)?

int main() 
{
    int a;
    std::cout << a;
}


推荐答案

存储持续时间不会在C ++中自动初始化为零。在C ++中,您不需要为不需要的东西付费,并且自动初始化变量需要时间(将存储器位置置零为最终减少为机器干扰,然后转换为控制物理位的电信号)。

That is because variables with automatic storage duration are not automatically initialized to zero in C++. In C++, you don't pay for what you don't need, and automatically initializing a variable takes time (setting to zero a memory location ultimately reduces to machine intruction(s) which are then translated to electrical signals that control the physical bits).

变量被保留一个内存位置,并且发生一些垃圾在那个内存位置。那个垃圾被 cout 打印出来。

The variable is being reserved a memory location, and it happens that some junk is at that memory location. That junk is being printed out by cout.

正如@dwcanillas指出的,这是未定义的行为。相关: C中已声明的未初始化变量会发生什么?是否有值?

As pointed out by @dwcanillas, it is undefined behaviour. Related: What happens to a declared, uninitialized variable in C? Does it have a value?

从C ++标准(强调我的):

From the C++ standard (emphasize mine):

strong> 8.5初始化程序[dcl.init]

8.5 Initializers [dcl.init]


7)默认初始化类型为T的对象



  • 如果T是(可能是cv限定的)类类型,构造函数是
    考虑的。枚举了适用的构造函数(13.3.1.3),通过重载分辨率(13.3)选择初始化器()的最佳

  • 如果T是一个数组类型,每个元素都是默认初始化的。
  • / li>
  • 否则,不执行初始化。

  • If T is a (possibly cv-qualified) class type (Clause 9), constructors are considered. The applicable constructors are enumerated (13.3.1.3), and the best one for the initializer () is chosen through overload resolution (13.3). The constructor thus selected is called, with an empty argument list, to initialize >> the object.
  • If T is an array type, each element is default-initialized.
  • Otherwise, no initialization is performed.



< 如果没有为某个对象指定初始值设定项,该对象将默认初始化。当获得具有自动或动态存储持续时间的对象的存储时,对象具有不确定的值,并且如果不对对象执行初始化,则该对象保持不确定的值,直到该值被替换(5.18)。 [注意:具有静态或线程存储持续时间的对象是零初始化的,请参见3.6.2。 - end note] 如果评估产生不确定的值,则除非在以下情况下,否则行为未定义

12) 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 (5.18). [Note: Objects with static or thread storage duration are zero-initialized, see 3.6.2. — end note ] If an indeterminate value is produced by an evaluation, the behavior is undefined except in the following cases:


- 如果通过以下评估产生无符号窄字符类型(3.9.1)的不确定值:

— If an indeterminate value of unsigned narrow character type (3.9.1) is produced by the evaluation of:

- 条件表达式的第二或第三个操作数(5.16),

— the second or third operand of a conditional expression (5.16),

- 逗号表达式的正确操作数(5.19),

— the right operand of a comma expression (5.19),

转换为无符号窄字符类型(4.7,5.2.3,5.2.9,5.4)或

— the operand of a cast or conversion to an unsigned narrow character type (4.7, 5.2.3, 5.2.9, 5.4), or

- 一个废值表达式(第5条)

— a discarded-value expression (Clause 5)

...


这篇关于为什么在C ++中打印一个未初始化的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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