为什么我会收到“警告:成员缺少初始化程序”? [-Wmissing场-初始化] [英] Why do I get "warning: missing initializer for member"? [-Wmissing-field-initializers]

查看:808
本文介绍了为什么我会收到“警告:成员缺少初始化程序”? [-Wmissing场-初始化]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么我会在一种情况下得到有关初始化的警告,但不是其他情况。代码位于C ++源文件中,并使用GCC 4.7和 -std = c ++ 11






  struct sigaction old_handler,new_handler; 

以上产生与 -Wall一起的 NO 警告 -Wextra






  struct sigaction old_handler = {},new_handler = {}; 
struct sigaction old_handler = {0},new_handler = {0};

以上产生警告:

<$ p $警告:缺少用于成员'sigaction :: __ sigaction_handler'的初始化器[-Wmissing-field-initializers]
警告:缺少用于成员'sigaction :: sa_mask'的初始化器[-Wmissing-field-初始值设定项]
warning:成员'sigaction :: sa_flags'缺少初始值设定项[-Wmissing-field-initializers]
warning:成员'sigaction :: sa_restorer'缺少初始值设定项[-Wmissing-field-initializers]






我已阅读如何正确地从C ++初始化C结构?为什么编译器会抛出此警告:缺少初始化" ;?是不是结构初始化?,以及像 http: //gcc.gnu.org/bugzilla/show_bug.cgi?id=36750 。我不明白为什么未初始化的struct 不是生成警告,而是初始化的struct 产生警告。



为什么未初始化的结构体没有生成警告;为什么初始化的结构体产生一个警告?解决方案

p>

  #include< iostream> 

struct S {
int a;
int b;
};

int main(){
S s {1}; // b将被自动设置为0
//并且这很可能(?)不是你想要的
std :: cout<<sa =<< s.a<< ,sb =<< s.b<< std :: endl;
}

它给出警告:


missing.cpp:在函数'int main()'中:

missing.cpp:9:11:warning:成员'S :: b缺少初始值设定项'[-Wmissing-field-initializers]


程序打印:


sa = 1,sb = 0

警告只是编译器提醒那 S 有两个成员,但你只明确地初始化其中的一个,另一个将被设置为零。如果这就是你想要的,你可以放心地忽略这个警告。



在这样一个简单的例子中,它看起来很愚蠢和烦人;如果你的结构有许多成员,那么这个警告可能是有用的(捕捉错误:错误的字段数或错别字)。






< blockquote>

为什么未初始化的结构不会产生警告?


会产生太多的警告。毕竟,这是合法的,如果您使用未初始化的成员,这只是一个错误。例如:

  int main(){
S s;
std :: cout<<s.a =<< s.a<<,s.b =<< s.b<< std :: endl;




missing.cpp:In function'int main ()':

missing.cpp:10:43:warning:'sS :: b'在此函数中未初始化使用[-Wuninitialized]

missing.cpp:10: 26:警告:'sS :: a'在此函数中未初始化使用[-Wuninitialized]


即使它没有警告我关于 s 的未初始化成员,它提醒我使用未初始化的字段。一切都好。


为什么初始化的结构会产生警告?


只有在您显式地部分初始化字段时,它才会发出警告。这是一个提醒,该结构比您列举的字段更多。在我看来,这个警告的用处是值得怀疑的:它的确会产生太多的误报。那么,它的默认情况下不会出于某种原因...


I'm wondering why I am getting an warning about initialization in one case, but not the other. The code is in a C++ source file, and using GCC 4.7 with -std=c++11.


struct sigaction old_handler, new_handler;

The above produces NO warnings with -Wall and -Wextra.


struct sigaction old_handler={}, new_handler={};
struct sigaction old_handler={0}, new_handler={0};

The above produces warnings:

warning: missing initializer for member ‘sigaction::__sigaction_handler’ [-Wmissing-field-initializers]
warning: missing initializer for member ‘sigaction::sa_mask’ [-Wmissing-field-initializers]
warning: missing initializer for member ‘sigaction::sa_flags’ [-Wmissing-field-initializers]
warning: missing initializer for member ‘sigaction::sa_restorer’ [-Wmissing-field-initializers]


I've read through How should I properly initialize a C struct from C++?, Why is the compiler throwing this warning: "missing initializer"? Isn't the structure initialized?, and bug reports like http://gcc.gnu.org/bugzilla/show_bug.cgi?id=36750. I don't understand why the uninitialized struct is not generating a warning, while the initialized struct is generating a warning.

Why is the uninitialized structs not generating a warning; and why is the initialized structs generating a warning?

解决方案

Here is a simple example:

#include <iostream>

struct S {
  int a;
  int b;
};

int main() {
  S s { 1 }; // b will be automatically set to 0
             // and that's probably(?) not what you want
  std::cout<<"s.a = "<<s.a<<", s.b = "<<s.b<<std::endl;
}

It gives the warning:

missing.cpp: In function ‘int main()’:
missing.cpp:9:11: warning: missing initializer for member 'S::b' [-Wmissing-field-initializers]

The program prints:

s.a = 1, s.b = 0

The warning is just a reminder from the compiler that S has two members but you only explicitly initialized one of them, the other will be set to zero. If that's what you want, you can safely ignore that warning.

In such a simple example, it looks silly and annoying; if your struct has many members, then this warning can be helpful (catching bugs: miscounting the number of fields or typos).


Why is the uninitialized structs not generating a warning?

I guess it would simply generate too much warnings. After all, it is legal and it is only a bug if you use the uninitialized members. For example:

int main() {
  S s;
  std::cout<<"s.a = "<<s.a<<", s.b = "<<s.b<<std::endl;
}

missing.cpp: In function ‘int main()’:
missing.cpp:10:43: warning: ‘s.S::b’ is used uninitialized in this function [-Wuninitialized]
missing.cpp:10:26: warning: ‘s.S::a’ is used uninitialized in this function [-Wuninitialized]

Even though it did not warn me about the uninitialized members of s, it did warn me about using the uninitialized fields. All is fine.

Why is the initialized structs generating a warning?

It warns you only if you explicitly but partially initialize the fields. It is a reminder that the struct has more fields than you enumerated. In my opinion, it is questionable how useful this warning is: It can indeed generate too much false alarms. Well, it is not on by default for a reason...

这篇关于为什么我会收到“警告:成员缺少初始化程序”? [-Wmissing场-初始化]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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