结构中未初始化的字段 [英] Uninitialised fields in structures

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

问题描述

问候!


我最近对编译器关于此代码的警告感到惊讶:


struct text {

char * s;

size_t len;

};

int main(){

struct text t = {" hello world!"};

}


编译器实际声称t.len未初始化。好吧,我没有明确初始化它,但我认为它应该是

初始化为零然后(即最后一个之后的所有字段都是

初始化为零)。好吧,这只是一个警告,所以我倾向于忽略

它。现在,当我通过Valgrind运行代码时,它还抱怨使用了

未初始化的值,这让我思考。最后,我使用gdb来完成代码并明确切碎了t.len的值,然后在

那行和 - 并且看到 - 它是正确的(恕我直言)重置为零!


现在,我非常确定附加字段的规则,但是我还是想知道
。有人可以确认或否认上面的t.len是否初始化是否为



谢谢


Uli

Greetings!

I was recently surprised by the compiler''s warning concerning this code:

struct text {
char* s;
size_t len;
};
int main() {
struct text t = {"hello world!"};
}

The compiler actually claimed that t.len was uninitialised. Okay, I don''t
explicitly initialise it, but I was under the impression that it should be
initialised to zero then (i.e. all fields after the last one are
initialised with zero). Okay, it''s just a warning, so I tended to ignore
it. Now, when I ran the code through Valgrind, it also complained that an
uninitialised value was used, which got me thinking. Lastly, I used gdb to
step through the code and explicitly shredded the value of t.len before
that line and - lo and behold - it was correctly (IMHO) reset to zero!

Now, I''m pretty sure about the rule with the additional fields, but I''m
wondering nonetheless. Can someone confirm or deny whether t.len above is
initialised or not?

thank you

Uli

推荐答案

12月31日下午12:58,Ulrich Eckhardt< dooms ... @ knuut.dewrote:
On Dec 31, 12:58 pm, Ulrich Eckhardt <dooms...@knuut.dewrote:

问候!

我最近对编译器关于此代码的警告感到惊讶:


struct text {

char * s;

size_t len;

};

int main (){

struct text t = {" hello world!"};

}


编译器实际声称t.len没有被初始化。好吧,我没有明确初始化它,但我认为它应该是

初始化为零然后(即最后一个之后的所有字段都是

初始化为零)。好吧,这只是一个警告,所以我倾向于忽略
Greetings!

I was recently surprised by the compiler''s warning concerning this code:

struct text {
char* s;
size_t len;
};
int main() {
struct text t = {"hello world!"};
}

The compiler actually claimed that t.len was uninitialised. Okay, I don''t
explicitly initialise it, but I was under the impression that it should be
initialised to zero then (i.e. all fields after the last one are
initialised with zero). Okay, it''s just a warning, so I tended to ignore



这对于一个数组来说也是如此。

That is true only for an array.


it。现在,当我通过Valgrind运行代码时,它还抱怨使用了

未初始化的值,这让我思考。最后,我使用gdb来完成代码并明确切碎了t.len的值,然后在

那行和 - 并且看到 - 它是正确的(恕我直言)重置为零!


现在,我非常确定附加字段的规则,但是我还是想知道
。有人可以确认或否认上面的t.len是否初始化是否为

it. Now, when I ran the code through Valgrind, it also complained that an
uninitialised value was used, which got me thinking. Lastly, I used gdb to
step through the code and explicitly shredded the value of t.len before
that line and - lo and behold - it was correctly (IMHO) reset to zero!

Now, I''m pretty sure about the rule with the additional fields, but I''m
wondering nonetheless. Can someone confirm or deny whether t.len above is
initialised or not?



标准没有说明将其余成员初始化

为0.

使用gdb确定一个程序是否正确是坏的

练习,因为它是运行程序并通过

输出得出结论。

Gdb不是''C99工具''。

The standard doesn''t say anything about initializing the rest members
to 0.
Using gdb to determine whether a program is correct or not is bad
practise, as it is to run the program and come to a conclusion by the
output.
Gdb is not a ''C99 tool''.


12月31日上午10:58 * am,Ulrich Eckhardt< dooms ... @ knuut。 dewrote:
On Dec 31, 10:58*am, Ulrich Eckhardt <dooms...@knuut.dewrote:

问候!


我最近对编译器关于此代码的警告感到惊讶:


*结构文本{

* * char * s;

* * size_t len;

*} ;

* int main(){

* * struct text t = {" hello world!"};

*}


编译器实际声称t.len未初始化。好吧,我没有明确初始化它,但我认为它应该是

初始化为零然后(即最后一个之后的所有字段都是

初始化为零)。
Greetings!

I was recently surprised by the compiler''s warning concerning this code:

* struct text {
* * char* s;
* * size_t len;
* };
* int main() {
* * struct text t = {"hello world!"};
* }

The compiler actually claimed that t.len was uninitialised. Okay, I don''t
explicitly initialise it, but I was under the impression that it should be
initialised to zero then (i.e. all fields after the last one are
initialised with zero).



的确如此。 n1256的第6.7.8段第21段规定:


如果

括号内的清单中的初始化程序数少于元素

或聚合的成员,或更少的字符串文字中的
字符用于

初始化已知大小的数组比

那里是数组中的元素,聚合的余数

应隐式初始化

与具有静态存储的对象相同

持续时间。
和同一条款的第10段规定

静态存储的算术类型初始化为0.

Indeed. Paragraph 21 of 6.7.8 of n1256 states:

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 paragraph 10 of the same clause states that
arithmetic types of static storage are initialized to 0.


好​​吧,这只是一个警告,所以我倾向于忽略

它。现在,当我通过Valgrind运行代码时,它还抱怨使用了

未初始化的值,这让我思考。
Okay, it''s just a warning, so I tended to ignore
it. Now, when I ran the code through Valgrind, it also complained that an
uninitialised value was used, which got me thinking.



关于编译器,或许它只是警告你没有显式初始化

。关于valgrind我

我不知道它是如何解决这些问题的。

Regarding the compiler perhaps it just warns you about
the lack of explicit initialization. Regarding valgrind I
don''t know how it deals with such issues.


现在,我很确定规则有了额外的领域,但是我还是想知道。有人可以确认或否认上面的t.len是否初始化是否为

Now, I''m pretty sure about the rule with the additional fields, but I''m
wondering nonetheless. Can someone confirm or deny whether t.len above is
initialised or not?



根据它应该的标准。也许编写你的编译器的那个人对于

标准的那一部分一无所知? ;-)

According to the standard it should. Perhaps the person
who wrote your compiler was ignorant about that part of
the standard ? ;-)


vi ****** @ gmail.com 写道:
vi******@gmail.com writes:

12月31日下午12:58,Ulrich Eckhardt< dooms ... @ knuut.dewrote:
On Dec 31, 12:58 pm, Ulrich Eckhardt <dooms...@knuut.dewrote:

>我最近对编译器关于此代码的警告感到惊讶:

结构文本{
char * s;
size_t len;
};
int main(){
struct text t = {" hello world!"};
}
编译器实际上声称t.len没有被初始化。好吧,我没有明确初始化它,但我的印象是它应该初始化为零(即最后一个之后的所有字段都用零初始化) 。
>I was recently surprised by the compiler''s warning concerning this code:

struct text {
char* s;
size_t len;
};
int main() {
struct text t = {"hello world!"};
}

The compiler actually claimed that t.len was uninitialised. Okay, I don''t
explicitly initialise it, but I was under the impression that it should be
initialised to zero then (i.e. all fields after the last one are
initialised with zero).



< snip>

<snip>


>现在,我很确定附加字段的规则,但我是
尽管如此。有人可以确认或否认上面的t.len是否初始化?
>Now, I''m pretty sure about the rule with the additional fields, but I''m
wondering nonetheless. Can someone confirm or deny whether t.len above is
initialised or not?



标准没有说明将其余成员初始化

为0.

The standard doesn''t say anything about initializing the rest members
to 0.



我认为确实如此。 6.7.8段。 21:


如果括号括起的列表中的初始值设定项少于那个

是聚合的元素或成员,或者是

字符串文字用于初始化已知大小的数组

是数组中的元素,聚合的其余部分应该是

隐式初始化与具有静态存储的对象相同

持续时间。


进一步向上(第10段)解释静态持续时间的对象是

默认情况下初始化为算术零或NULL。


-

Ben。

I think it does. 6.7.8 para. 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.

Further up (para. 10) explains that objects of static duration are
initialised to arithmetic zero or NULL by default.

--
Ben.


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

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