http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53119
Bug #: 53119
Summary: -Wbraces wrongly warns about universal zero
initializer {0}
Classification: Unclassified
Product: gcc
Version: unknown
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
AssignedTo: [email protected]
ReportedBy: [email protected]
In C, {0} is the universal zero initializer equivalent to C++'s {} (the latter
being invalid in C). It is necessary to use whenever you want a
zero-initialized object of a complete but conceptually-opaque or
implementation-defined type. The classic example in the C standard library is
mbstate_t:
mbstate_t state = { 0 }; /* correctly zero-initialized */
versus the common but nonportable:
mbstate_t state;
memset(&state, 0, sizeof state);
In this case, gcc -Wbraces (which is included in -Wall) actively discourages
the programmer from writing the correct form of the code by throwing ugly
warnings.
Note that if you want to eliminate warnings and write portable code, the *only*
way to zero-initialize an object like this is:
static const mbstate_t zero_state;
mbstate_t state = zero_state;
(i.e. creating an extra object of static storage duration to be implicitly zero
initialized).
The same reasoning applies to any structures provided by third-party libraries
which must be allocated by the calling application and zero-initialized, but
whose definitions are intended to be considered as opaque.
To fix the issue, GCC should simply special-case {0} as always-valid and
suppress warnings for it, while leaving in effect all other behaviors of
-Wbraces.