简体   繁体   English

C ++结构初始化全部为零

[英]C++ structure initialization with all zeros

In C++ if I initialize the structure in the form of "= {}", as in example below, does it ensure to assign values zero to all the member of the structure? 在C ++中如果我以“= {}”的形式初始化结构,如下例所示,它是否确保为结构的所有成员赋值为零? I understand this seem duplicate question, But my question also is if it initializes zero to all members, does it also apply for complex structure ? 我理解这似乎是重复的问题,但我的问题是,如果它对所有成员初始化为零,它是否也适用于复杂的结构? Like structure within structure , or for this each member has to be explicitly assigned value zero in the code?. 像结构中的结构一样,或者为此,每个成员必须在代码中明确赋值为零?


typedef struct s{
      int i;
      bool x;
 };

  int main ()
 {
     s initial = {};
     printf("%d %d", initial.i, initial.x);

 }

Edit: To reference complex structure, 编辑:参考复杂的结构,

typedef struct scomplex{
    s initial;
    s t[5];
 };
  int main (void)
  {
     scomplex sc = {};
     printf ("%d %d %d",sc.initial.i, sc.initial.x, sc.t[0].i);  
  }

But my question also is if it initializes zero to all members, does it also apply for complex structure ? 但我的问题是,如果它对所有成员初始化为零,它是否也适用于复杂的结构?

Yes, all members will be initialized, including "complex" member, but might not be initialized to zero, the final effect is determined by their types. 是的,所有成员都将被初始化,包括“复杂”成员,但可能不会被初始化为零,最终效果取决于他们的类型。

According to your sample code, struct s is an aggregate type, then aggregate initialization is performed. 根据您的示例代码,struct s是聚合类型,然后执行聚合初始化

(emphasis mine) (强调我的)

If the number of initializer clauses is less than the number of members and bases (since C++17) or initializer list is completely empty , the remaining members and bases (since C++17) are initialized by their default initializers, if provided in the class definition, and otherwise (since C++14) by empty lists, in accordance with the usual list-initialization rules (which performs value-initialization for non-class types and non-aggregate classes with default constructors, and aggregate initialization for aggregates ). 如果初始化程序子句的数量小于成员and bases (since C++17)初始化程序列表完全为空 ,则其余成员and bases (since C++17) by their default initializers, if provided in the class definition, and otherwise (since C++14)空列表,按照通常的列表初始化规则( 对非类型类和非聚合类使用默认构造函数执行值初始化,以及聚合初始化)对于聚合 )。

For this case the member i and x of struct s will be value initialized to zero. 对于这种情况,struct s的成员ix将被初始化为零。

4) otherwise, the object is zero-initialized. 4)否则,对象是零初始化的。

If struct s has any other members, they'll be initialized (value initialized or aggregate initialized according to their types) by empty lists recursively. 如果结构s有任何其他成员,他们会进行初始化(值初始化还是要根据自己的类型合计初始化)通过递归空列表。

EDIT 编辑

For your added sample (struct scomplex ), the member initial will be value initialized , and the final effect depends on the type s . 对于添加的样本(struct scomplex ),成员initial 值将初始化值 ,最终效果取决于类型s And another member is an array, which will be aggregate initialized with empty list, and all the elements of the array will be value initialized; 另一个成员是一个数组,它将使用空列表进行聚合初始化 ,并且数组的所有元素都将被初始化值; Same as member initial , the effect depends on the type s . 与成员initial相同,效果取决于类型s

Problem 问题

Will this initialize all of the members to 0? 这会将所有成员初始化为0吗?

typedef struct s{
      int i;
      bool x;
 };

int main ()
{
     s initial = {};
     printf("%d %d", initial.i, initial.x);

}

Answer: yes. 答:是的。 Proof? 证明? Here you can see it become 0. 在这里你可以看到它变为0。

Better Alternatives? 更好的替代品?

This is an opinionated section. 这是一个自以为是的部分。 But In My Opinion (IMO), initializing it with {0} would be more readable than {} , as it notifies the user of the 0. It is actually being filled up with 0's. 但在我的意见(IMO)中,用{0}初始化它将比{}更具可读性,因为它通知用户0.它实际上正在填充0。

s initial = {0};

What is this called? 这个叫什么?

This is called Aggregate Initialization , as Dieter Lücking defined, or Value Initialization , as songyuanyao noted. 这被称为Aggregate Initialization ,正如DieterLücking定义的那样,或者Value Initialization ,正如songyuanyao指出的那样。 It's basically a form of initialization where you can initialize a struct with values you would like. 它基本上是一种初始化形式,您可以使用您想要的值初始化结构。 For example, let's initialize it with the value 1 instead of 0! 例如,让我们用值1而不是0来初始化它! You would do: 你会这样做:

// Example program
#include <stdio.h>
#include <iostream>


typedef struct s{
 int i;
 bool x;
};

int main ()
{
   s initial = {1,1};
   printf("%d %d", initial.i, initial.x);
}

You can see this compiled here . 你可以在这里看到这个。 As you can see above, I am doing 1,1 which is normal initialization. 如上所示,我正在做1,1这是正常的初始化。 As opposed to 0 initialization, you can't just initialize all the parts of the struct as easily as you can with 0. 与0初始化相反,您不能像使用0一样轻松地初始化结构的所有部分。

References 参考

cpprefrence cpprefrence

what is aggregate initialization 什么是聚合初始化

What do the following phrases mean in C++: zero-, default- and value-initialization? 以下短语在C ++中的含义是什么:零,默认和值初始化?

Glossary 词汇表

Aggregate Initialization : 聚合初始化

Aggregate initialization is a form of list-initialization, which initializes aggregates. 聚合初始化是列表初始化的一种形式,它初始化聚合。

Value Initialization : 价值初始化

Initialize values 初始化值

This is the initialization performed when a variable is constructed with an empty initializer. 这是使用空初始化程序构造变量时执行的初始化。

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

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