简体   繁体   English

int x; int y; int * ptr; 是不是初始化,对吧?

[英]int x; int y; int *ptr; is not initialization, right?

I'm reading 'C++ All-in-One for Dummies' by JP Mueller and J. Cogswell and stumbled onto this: 我正在阅读JP Mueller和J. Cogswell撰写的“C ++ All-in-One for Dummies”并偶然发现:

#include <iostream>
using namespace std;
int main()
{
    int ExpensiveComputer;
    int CheapComputer;
    int *ptrToComp;
...

This code starts out by initializing all the goodies involved — two integers and a pointer to an integer. 这段代码首先初始化所有涉及的东西 - 两个整数和一个指向整数的指针。

Just to confirm, this is a mistake and should read '... by declaring', right? 只是为了确认,这是一个错误,应该读作'......通过声明',对吧? It's just strange to me that such basic mistakes still make their way to books. 对我来说,这样的基本错误仍然会出现在书中,这一点很奇怪。

From the point of view of the language, this is default initialization . 从语言的角度来看,这是默认初始化 The problem is, they are initialized to indeterminate values. 问题是,它们被初始化为不确定的值。

otherwise, nothing is done: the objects with automatic storage duration (and their subobjects) are initialized to indeterminate values. 否则,什么都不做:具有自动存储持续时间的对象(及其子对象)被初始化为不确定值。

Default initialization of non-class variables with automatic and dynamic storage duration produces objects with indeterminate values (static and thread-local objects get zero initialized) 具有自动和动态存储持续时间的非类变量的默认初始化会生成具有不确定值的对象(静态和线程局部对象初始化为零)

Note that any attempt to read these indeterminate values leads to UB . 请注意,任何读取这些不确定值的尝试都会导致UB

From the standard, [dcl.init]/7 从标准来看, [dcl.init] / 7

To default-initialize an object of type T means: 默认初始化T类型的对象意味着:

  • If T is a (possibly cv-qualified) class type ([class]), constructors are considered. 如果T是(可能是cv限定的)类类型([class]),则考虑构造函数。 The applicable constructors are enumerated ([over.match.ctor]), and the best one for the initializer () is chosen through overload resolution ([over.match]). 枚举适用的构造函数([over.match.ctor]),并通过重载决策([over.match])选择初始化程序()的最佳构造函数。 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. 如果T是数组类型,则每个元素都是默认初始化的。

  • Otherwise, no initialization is performed. 否则,不执行初始化。

Yes you are correct. 是的,你是对的。

You declared and defined these variables, you did not initialize them! 声明定义这些变量,你没有初始化它们!

PS: What is the difference between a definition and a declaration? PS: 定义和声明有什么区别?

This code both declares and defines three variables but does not initialize them (their values are said to be indeterminate ). 这段代码既声明定义了三个变量,但没有初始化它们(它们的值被认为是不确定的 )。

A variable declaration only must include keyword extern . 变量声明只能包含关键字extern

Right. 对。 Hence, "dummies". 因此,“假人”。 :) :)

We can't even blame this on legacy; 我们甚至不能将这归咎于遗产; historically C programmers would declare* a variable and then "initialize" it later with its first assignment. 历史上,C程序员会声明*变量,然后在第一次赋值时“初始化”它。

But it was never the case that simply declaring a variable, without an initializer, were deemed to be "initializing" it.** 但是,如果没有初始化程序,简单地声明一个变量就不会被视为“初始化”它。**

So the wording is just wrong. 所以措辞是错的。

* Technically we're talking about definitions , but when we say "declare a variable" we almost always mean defining declarations. *从技术上讲, 我们谈论的是定义 ,但当我们说“声明变量”时,我们几乎总是指定义声明。

** Though objects with static storage duration do undergo their own zero-initialisation phase before anything else happens, so forgoing initialisation yourself is not a catastrophe in that case. **虽然具有静态存储持续时间的对象在其他任何事情发生之前都会经历自己的零初始化阶段,因此在这种情况下,自己进行初始化并不是一个灾难。 Still, we cannot claim that we have initialised that object. 但是,我们不能声称我们已经初始化了该对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM