How to remove the warning in gcc 4.6: missing initializer [-Wmissing-field-initializers]?
Solution 1
Use G_VALUE_INIT
to initialize GValue
-s. Their (private) structure is in /usr/include/glib-2.0/gobject/gvalue.h
which #define G_VALUE_INIT
appropriately.
I strongly disagree with your assessment that it is GCC's bug. You ask to be warned if a field is not explicitly initialized with -Wmissing-field-initializers
and you get the warning you deserve.
Sadly G_VALUE_INIT
is not documented, but it is here. Code with
GValue value = G_VALUE_INIT;
There is no universal solution to never get the warning about missing field initialization if -Wmissing-field-initializers
is asked. When you ask for such a warning, you require the compiler to warn of every incomplete initializers. Indeed, the standard requires than all the non-explicitly initialized struct
fields be zeroed, and gcc
obeys the standard.
You could use diagnostic pragmas like
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
But my feeling is that you should code with care, and explicitly initialize all the fields. The warning you get is more a coding style warning (maybe you forgot a field!) than a bug warning.
I also believe that for your own (public) struct
you should #define
an initializing macro, if such struct
are intended to be initialized.
Solution 2
You could use:
-Wno-missing-field-initializers
to inhibit that warning specifically. Conversely, you could make it into an error with:
-Werror=missing-field-initializers
Both of these work with GCC 4.7.1; I believe they work with GCC 4.6.x too, but they don't work with all earlier versions of GCC (GCC 4.1.2 recognizes -Wno-missing-field-initializers
but not -Werror=missing-field-intializers
).
Obviously, the other way to suppress the warning is to initialize all fields explicitly. That can be painful, though.
Solution 3
It also appears that using the .field-style of initialization, such as:
GValue value = { .somefield = 0 };
will cause the compiler to not issue the warning. Unfortunately if the struct is opaque, this is a non-starter.
Jack
Computer lover. :-) -- I'm not a native speaker of the english language. So, if you find any mistake what I have written, you are free to fix for me or tell me on. :)
Updated on June 14, 2022Comments
-
Jack almost 2 years
The code:
GValue value = { 0 };
Give the following warning:
missing initializer [-Wmissing-field-initializers]
I know that's a
gcc's BUG
; but is there some trick to remove it? really not nice see such unreal warnings. But I don't want power off the warning because it will hidden real warnings from me too. A sorry, but I can't update my gcc to 4.7(where looks like it was fixed) version, yet. -
jww over 11 years"I strongly disagree with your assessment that it is GCC's bug" - Agreed. Taking it a step further, initialize everything and let the optimizer discard spurious loads. It gets old tracking down these sorts of bugs because a programmer is trying to be clever (and the memory manager did not serve up a page that was zero'd).
-
pwseo almost 11 yearsBut it is a bug.
{0}
is what's called the "universal zero initializer", and when one uses it, one is explicitly initializing all fields to their logical zero. It thus makes no sense for GCC (or Clang, or any other compiler) to emit such a warning on such cases. -
Lundin about 10 years@pwseo Old post, but your statement is not correct. The C language does not treat
{0}
as a special case. There is no such thing as an "universal zero initializer" nor is{0}
a way to explicitly initialize the whole array/struct.{0}
simply means: initialize the very first element to zero, and then let every other member get initialized just as if they had static storage duration, which is done implicitly. And that is why{1}
only initializes the first element to 1 and the rest to zero, it works exactly the same. -
aberaud over 9 years@Lundin indeed,
{0}
is not the "universal zero initializer", but{}
IS the "universal initializer", that will, as per the standard, always fill a structure with 0 for fields with no user-defined initializers/no constructor. This language facility is often used because 0 is what the programmer usually wants. Many times this is used with C structures with (obviously) no initializer, and no possibility to add ones. The warning is a (very annoying) GCC bug. Why adding adding non-required, non-useful constraints that will distract the programmer from the actual stuff ? -
Lundin over 9 years@aberaud Huh? Empty initializer lists are not allowed in C, see C11 6.7.9 or read this. Also, constructors are of no concern, as there are no constructors in the C language.
-
aberaud over 9 years@Lundin Yes, that is the point: C structures (with no constructors/initializers) can be 0-initialized in C++ by using
{}
, and that is totally valid and standard-compliant, because the code is compiled as C++, not C. -
Philip Withnall over 5 yearsYou can use
{0,}
(note the comma) to initialise all fields to zero, but it will choke on some struct layouts. This is as close as C gets to a ‘universal zero initialiser’. If you really want a universal zero initialiser, usememset()
on the struct instance. -
Matthijs Kooijman over 4 yearsWith recent gcc versions, this might no longer be true (I've seen the same warning on a different codebase using a designated initializer like you show, with arm-gcc 8).
-
John Hascall over 4 yearsInteresting. IIRC the C standard requires that all unnamed fields be initialized to their zero value with this construct. So there are no uninitialized fields.
-
Matthijs Kooijman over 4 yearsI believe it does zero-initialize, just warns about the missing explicit initializers. Also, it seems this warning only happens for C++, not C code. See also gcc.gnu.org/bugzilla/show_bug.cgi?id=39589