Why does the compiler throw this warning: "Missing initializer"? Is the structure initialized?

I am creating some kind of interface for the program. To run the program, I use the CreateProcess() call, which, among other things, gets a pointer to the STARTUPINFO structure. To initialize the structure that I used:

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

When compiling a program with GCC enabled with these warning sets -Wall -Wextra this gives me a warning that there is no initializer pointing to the first line.

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

So I finished:

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

And thus the compiler does not give any warnings. The question is, what is the difference between these methods of initializing the structure? Using the first method, is the structure initialized? Which one would you recommend?

+56
c gcc winapi
Oct 08 '09 at 16:26
source share
5 answers

GCC is just too paranoid - for some good reason, in my opinion, but then it’s certainly true that GCC contributors know a lot more about the C nuances that I do.

See this little discussion about the issue on the GCC mailing list:

The bottom line, though - initializing the structure with just {0} will actually zero initialize all of this.

The C99 standard states the following in 6.7.8 / 21 “Initialization - Semantics”:

If the list enclosed in brackets contains fewer initializers than the element or elements of the collection or fewer characters in the string literal used to initialize an array with a known size than in the array, the rest of the aggregate must be initialized implicitly in the same way as objects, having a static storage duration.

C90 says it is slightly different in 6.5.7 in the same form (in other words, C99 hasn't added anything new here).

Also note that in C ++ this was expanded, so an empty set of curly brackets " {} " would initialize the value for the object, because there were situations (for example, patterns) when you did not even know what the members or the number members, which may be of type. Thus, this is not only good practice, but also the need to sometimes have a list of initializers, which is less than the number of members that objects can have.

+71
Oct 08 '09 at 17:01
source share

This can be easily fixed for GCC in C ++ programs by initializing the structure as

 STARTUPINFO startupInfo = STARTUPINFO(); 
  • just did it a few days ago
+13
09 Oct '09 at 14:15
source share

You requested as many warnings as possible using -Wall -Wextra.

In this case, you will receive a warning that indicates that you did not specify all the fields, which is completely true, but could be unintentional.

You can suppress this warning by adding -Wno missing-field initializers

+9
Mar 22 '11 at 12:31
source share

This web page details key issues: http://ex-parrot.com/~chris/random/initialise.html

As a working solution, my current solution is to selectively suppress this warning:

 #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wmissing-field-initializers" STARTUPINFO startupInfo = {0}; #pragma clang diagnostic pop 

Unfortunately, this only works in clang and doesn't seem to work in GCC.

+6
Feb 15 '12 at 12:08
source share

In C ++, you can use boost::initialized_value to get rid of this warning. I have warnings for boost ; so I don’t know if this will lead to other warnings in your case. This way you do not need to turn off the warning.

Example:

 T bla = boost::initialized_value; 
+1
Sep 09 '13 at 17:02
source share



All Articles