结构初始化 [英] Structure initialization

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

问题描述

最近,我在代码审查中发现了我使用

初始化方法

AStruct myStruct = {0};


将myStruct的所有元素初始化为0.


我被问及,因为没有人看过这个结构。

所以我看了它在我在这里找到的指针

的指针中尽我所能,并且找不到对这个

构造的任何具体引用。我找到的是参考(释义)任何值初始化器中未指定的
初始化为零。


所以似乎construct = { 0},将fisrst元素设置为0和

,因为不再存在初始值设定项,所有其他结构

成员默认设置为零。这是正确的吗?


谢谢

Joe

Recently I was pinged in a code review about my use of the
initialization method
AStruct myStruct = { 0 } ;

Which initializes all elements of the myStruct to 0.

I was questioned on it because no one had seen this construct.
So i looked it up as best i could in the pointers to the Specification
that I find here and could not find any specific reference to this
construct. What i did find was reference to (paraphrasing) any values
not specified in the initializer are initialized to zero.

so it seems that the construct ={0}, sets the fisrst element to 0 and
since there are no more initializers present, all other structure
members are set to zero by default. Is this correct ?

Thanks
Joe

推荐答案

6月10日,12:53 pm,jb.si ... @ lmco.com写道:
On Jun 10, 12:53 pm, jb.si...@lmco.com wrote:

最近我被关于我使用

初始化方法

AStruct myStruct = {0};


将myStruct的所有元素初始化为0.


我接受了质疑,因为没有人看到过这种结构。

所以我在规格指针中尽可能地查看了

我在这里找到并且找不到任何关于这个

构造的具体参考。我找到的是参考(释义)任何值初始化器中未指定的
初始化为零。


所以似乎construct = { 0},将fisrst元素设置为0和

,因为不再存在初始值设定项,所有其他结构

成员默认设置为零。这是正确的吗?


谢谢

Joe
Recently I was pinged in a code review about my use of the
initialization method
AStruct myStruct = { 0 } ;

Which initializes all elements of the myStruct to 0.

I was questioned on it because no one had seen this construct.
So i looked it up as best i could in the pointers to the Specification
that I find here and could not find any specific reference to this
construct. What i did find was reference to (paraphrasing) any values
not specified in the initializer are initialized to zero.

so it seems that the construct ={0}, sets the fisrst element to 0 and
since there are no more initializers present, all other structure
members are set to zero by default. Is this correct ?

Thanks
Joe



排序。这是'n1124的语言:


" 6.7.8。 21如果括号括起来的清单中的初始化程序数较少



元素或聚合成员或字符串中的字符数更少

literal使用

初始化一个已知大小的数组,而不是

数组中的元素,

剩余的聚合应该是隐式地初始化

作为

具有静态存储持续时间的对象。


和关于静态对象初始化的部分:


" 10如果一个具有自动存储持续时间的对象没有明确初始化
,那么它的值是不确定的。如果一个具有静态存储空间持续时间的对象没有明确初始化,那么:

?如果它有指针类型,则将其初始化为空指针;

?如果它有算术类型,则初始化为(正数或

无符号)零;

?如果它是一个聚合,每个成员都按照这些规则初始化(递归)

;

?如果它是一个联盟,那么根据这些规则的

初始化第一个命名成员

(递归)。

Sort of. Here''s the language from n1124:

"6.7.8. 21 If there are fewer initializers in a brace-enclosed list
than there are
elements or members of an aggregate, or fewer characters in a string
literal used
to initialize an array of known size than there are elements in the
array, the
remainder of the aggregate shall be initialized implicitly the same
as objects that
have static storage duration."

And the section on initialization of static objects:

"10 If an object that has automatic storage duration is not
initialized explicitly,
its value is indeterminate. If an object that has static storage
duration is not
initialized explicitly, then:
? if it has pointer type, it is initialized to a null pointer;
? if it has arithmetic type, it is initialized to (positive or
unsigned) zero;
? if it is an aggregate, every member is initialized (recursively)
according to these rules;
? if it is a union, the first named member is initialized
(recursively) according to
these rules."


在文章< 13 *********************************** @ 26g2000hsk.g ooglegroups.com> ,

< jb ****** @ lmco.comwrote:
In article <13**********************************@26g2000hsk.g ooglegroups.com>,
<jb******@lmco.comwrote:

>最近我被关于代码审查我使用
初始化方法
AStruct myStruct = {0};
>Recently I was pinged in a code review about my use of the
initialization method
AStruct myStruct = { 0 } ;


>其中将myStruct的所有元素初始化为0.
>Which initializes all elements of the myStruct to 0.


>我被问及,因为没有人看过这个结构。
>I was questioned on it because no one had seen this construct.



它做到了你所期望的,而且,我相信,经验丰富的C程序员认可了




另一方面,代码审查通常对于捕获构造来说很有用

对于将要求工作的学生程序员来说可能不清楚

几年后的代码。


在我看来,代码是正确的,更改

代码以显式初始化所有值没有任何价值以防万一某人

不懂代码;显式初始化只会导致
成为潜在错误的来源。但是对于未来的代码维护,

放入评论表明整个

结构正在被初始化是不会有害的;注释代码包含一个参考

到标准的特定部分,对于那些可能不太相信代码错误的人来说。

-

让我住在我旁边的房子里 -

这是男人们的竞争所在。 >
他们很好,他们很糟糕,他们很弱,他们很坚强

聪明,愚蠢 - 我也是如此; - Sam Walter Foss

It does what you expected, and is, I believe, recognized by
experienced C programmers.

On the other hand, code reviews are often useful to catch constructs
that might be unclear to the student programmer who is going to
be asked to work on the code several years from now.

In my opinion, the code is correct and there is no value in changing
the code to explicitly initialize all the values "just in case" someone
doesn''t understand the code; the explicit initialization would just
become a source of potential errors. But for future code maintenance,
it wouldn''t hurt to put in a comment indicating that the entire
structure was being initialized; the comment code include a reference
to the specific section of the Standard for those who might be
a little too convinced that the code is mistaken.
--
"Let me live in my house by the side of the road --
It''s here the race of men go by.
They are good, they are bad, they are weak, they are strong
Wise, foolish -- so am I;" -- Sam Walter Foss


6月10日下午1点53分,jb.si ... @ lmco.com写道:
On Jun 10, 1:53 pm, jb.si...@lmco.com wrote:

最近我在关于我使用

初始化方法的代码审查中进行了检查

AStruct myStruct = {0};


将myStruct的所有元素初始化为0.


我被问及,因为没有人看过这个结构。

所以我在指向

的指针中尽可能地查找了我在这里找到并且找不到任何具体的参考这个

构造。我找到的是参考(释义)任何值初始化器中未指定的
初始化为零。


所以似乎construct = { 0},将fisrst元素设置为0和

,因为不再存在初始值设定项,所有其他结构

成员默认设置为零。这是正确的吗?


谢谢

Joe
Recently I was pinged in a code review about my use of the
initialization method
AStruct myStruct = { 0 } ;

Which initializes all elements of the myStruct to 0.

I was questioned on it because no one had seen this construct.
So i looked it up as best i could in the pointers to the Specification
that I find here and could not find any specific reference to this
construct. What i did find was reference to (paraphrasing) any values
not specified in the initializer are initialized to zero.

so it seems that the construct ={0}, sets the fisrst element to 0 and
since there are no more initializers present, all other structure
members are set to zero by default. Is this correct ?

Thanks
Joe



我认为你是对的。唯一的例外我能想到的是

指针成员将被初始化为一个空指针常量,它可能不是全位零,但也许一个空指针常量可以

被视为一种特殊的零。


I think you are right. The only "exception" I can think of is that
pointer members will be initialized to a null pointer constant which
may not be all-bit zero, but maybe a null pointer constant can be
considered as just a special kind of zero.


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

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