Saturday, July 16, 2022

[FIXED] How to remove the warning in gcc 4.6: missing initializer [-Wmissing-field-initializers]?

Issue

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.


Solution

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.



Answered By - Basile Starynkevitch
Answer Checked By - Marilyn (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.