为什么编译器会抛出此警告:“缺少初始化程序"?结构不是初始化了吗? [英] Why is the compiler throwing this warning: "missing initializer"? Isn't the structure initialized?

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

问题描述

我正在为程序创建某种前端.为了启动程序,我使用了调用 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 ''.
startupInfo.cb = sizeof(startupInfo);

当使用 GCC 编译程序并启用这些警告集 -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')

所以我最终做了:

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 "Initialization - Sematics" 中有如下说明:

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 在 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 value 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天全站免登陆