为什么编译器抛出这样的警告:"缺少初始化"?是不是结构初始化? [英] Why is the compiler throwing this warning: "missing initializer"? Isn't the structure initialized?

查看:178
本文介绍了为什么编译器抛出这样的警告:"缺少初始化"?是不是结构初始化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一些前端的一个程序。要启动我用电话的CreateProcess()程序,除其他外接收指向一个 STARTUPINFO 结构体。要初始化我用来做结构:

I'm creating some sort of frontend for a program. To launch the program I'm using the call CreateProcess(), which among other things receives a pointer to a STARTUPINFO structure. To initialize the structure I used to do:

STARTUPINFO startupInfo = {0}; // Or even '\0'.
startupInfo.cb = sizeof(startupInfo);

在与海湾合作委员会使这些套警告 -Wall -Wextra 它给了我一个警告说,有指向的第一行缺少初始化编译程序。

When compiling the program with GCC enabling these sets of warnings -Wall -Wextra it gives me a warning saying that there's a missing initializer pointing to the first line.

warning: missing initializer
warning: (near initialization for 'startupInfo.lpReserved')

所以我落得这样做:

So I ended up doing:

STARTUPINFO startupInfo;
memset(&startupInfo, 0, sizeof(startupInfo));
startupInfo.cb = sizeof(startupInfo);

和这样的编译器不会给出任何警告。
现在的问题是,什么是初始化结构的这些方式之间的区别?
使用第一种方法,是不是结构初始化?
你会推荐哪一个?

And this way the compiler doesn't give any warning. The question is, what is the difference between these ways of initializing a structure? Using the first method, isn't the structure initialized? Which one would you recommend?

推荐答案

GCC只是过于偏执 - 在我看来没有充分的理由,但它肯定不错,GCC的维护者知道很多关于C的细微差别我做的。

GCC is just being overly paranoid - for no good reason in my opinion, but then it's certainly true that the GCC maintainers know a lot more about the nuances of C that I do.

请参阅讨论这个小线头关于GCC邮件列表上的问题:

See this small thread of discussion about the problem on the GCC mailing list:

底线,虽然 - 初始化与结构只是 {0} 实际上将零初始化整个事情

Bottom line though - initializing the struct with just {0} will in fact zero initialize the whole thing.

C99标准说,在6.7.8 / 21初始化 - 语义学以下内容:

The C99 standard says the following in 6.7.8/21 "Initialization - Sematics":

如果有一个大括号括起来的列表更少的初始化值多于使用比在数组中的元素来初始化已知大小的数组元素或聚集的成员,或者更少的字符的字符串,则剩余合计应初始化隐含一样具有静态存储持续时间的对象。

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.

C90说esentially 6.5.7中带着几分不同的措辞相同(换句话说,C99并没有增加新的东西在这里)。

C90 says esentially the same in 6.5.7 with a bit different wording (in other words, C99 didn't add something new here).

另外请注意,在C ++中,这被延长,这样一个空的大括号, {} ,将一个对象上执行默认初始化,因为有情况(如模板),当你甚至不知道什么是会员或有多少成员类型可能有。所以它不仅是很好的做法,但在必要时候有一个初始化列表,这比一个对象可能有成员数量越短。

Also note that in C++ this was extended so that an empty set of braces, "{}", would perform default initialization on an object because there were situations (like templates) when you wouldn't even know what the members or how many members a type might have. So not only is it good practice, but necessary at times to have an initializer list that's shorter than the number of members an object might have.

这篇关于为什么编译器抛出这样的警告:"缺少初始化"?是不是结构初始化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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