Bug 1110870 - -Wextra produces 'missing initializer' warnings when {0} struct initializer used for object containing aggregate members
Summary: -Wextra produces 'missing initializer' warnings when {0} struct initializer u...
Alias: None
Product: Red Hat Enterprise Linux 6
Classification: Red Hat
Component: gcc
Version: 6.4
Hardware: x86_64
OS: Linux
Target Milestone: rc
: ---
Assignee: Jakub Jelinek
QA Contact: qe-baseos-tools-bugs
Depends On:
TreeView+ depends on / blocked
Reported: 2014-06-18 16:12 UTC by Jason Vas Dias
Modified: 2014-06-18 16:25 UTC (History)
1 user (show)

Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Clone Of:
Last Closed: 2014-06-18 16:25:49 UTC
Target Upstream Version:

Attachments (Terms of Use)

Description Jason Vas Dias 2014-06-18 16:12:46 UTC
Description of problem:

Use of the '{0}' initializer on structure objects which have aggregate members
produces a warning when compiled with -Wextra.


file a.c :
struct s { int a ; char *b; struct { float c; char d ; } e; char f ; };
typedef struct s t;
void f(void)
  t v={0};

$ gcc -std=c99 -Wall -Wextra -c a.c
a.c: In function 'f':
a.c:5: warning: missing initializer
a.c:5: warning: (near initialization for 'v.b')
a.c:5: warning: unused variable 'v'

According to C99 (n1124.pdf), section 6.7.8 paragraph 21:
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.

Indeed, gcc-4.8.x emits only the "unused variable 'v'" warning with the same
command line, so this appears to be fixed upstream.

Warning such as this get me worried - does the object truly contain an
uninitialized 'b' member, or is the warning bogus ?

Version-Release number of selected component (if applicable):


$ gcc -v | tail -n 1:
gcc version 4.4.7 20120313 (Red Hat 4.4.7-3) (GCC)

How reproducible:


Steps to Reproduce:
try to compile code containing a partial structure initializer of an 
object containing aggregate members

Actual results:
gcc emits 'missing initializer' warning with -Wextra.

Expected results:
gcc should be fully initializing the structure object's memory to all 0's and
not emitting a warning in this case, even with -Wextra.

Additional info:

Comment 1 Jason Vas Dias 2014-06-18 16:22:34 UTC
oops, forget about 'aggregate members' in description above - it actually
occurs for any struct containing more than one member.

Comment 2 Jakub Jelinek 2014-06-18 16:25:49 UTC
This was changed with http://gcc.gnu.org/PR36750, but I'd say it is highly undesirable to change the warnings in an existing gcc release.  -Wextra certainly includes various warnings that go beyond what the standard mandates, the exception made for PR36750 was for C only and only for { 0 } initializers, e.g. if you write { 0, 0 }; even latest GCC will still complain about missing initializers.  It is true they are initialized with zero, on the other side not always is that the intended thing, often just some initializers were forgotten (say, you have a structure, write initializer for it when it has 2 fields, then add another field and forget to add initializer for that).
See -Wmissing-field-initializers documentation.

Note You need to log in before you can comment on or make changes to this bug.