public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/53119] New: -Wbraces wrongly warns about universal zero initializer {0}
@ 2012-04-25 17:04 bugdal at aerifal dot cx
  2012-04-25 17:53 ` [Bug c/53119] " manu at gcc dot gnu.org
                   ` (22 more replies)
  0 siblings, 23 replies; 24+ messages in thread
From: bugdal at aerifal dot cx @ 2012-04-25 17:04 UTC (permalink / raw)
  To: gcc-bugs

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: unassigned@gcc.gnu.org
        ReportedBy: bugdal@aerifal.cx


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.


^ permalink raw reply	[flat|nested] 24+ messages in thread

end of thread, other threads:[~2015-03-16  4:32 UTC | newest]

Thread overview: 24+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-04-25 17:04 [Bug c/53119] New: -Wbraces wrongly warns about universal zero initializer {0} bugdal at aerifal dot cx
2012-04-25 17:53 ` [Bug c/53119] " manu at gcc dot gnu.org
2012-04-25 18:02 ` [Bug c/53119] -Wmissing-braces " bugdal at aerifal dot cx
2012-04-25 18:13 ` manu at gcc dot gnu.org
2012-04-25 18:32 ` bugdal at aerifal dot cx
2012-04-25 22:13 ` manu at gcc dot gnu.org
2012-04-26 10:37 ` joseph at codesourcery dot com
2012-04-26 10:46 ` manu at gcc dot gnu.org
2012-04-26 10:51 ` joseph at codesourcery dot com
2012-05-03 17:55 ` manu at gcc dot gnu.org
2013-08-14 11:36 ` jnahughes at googlemail dot com
2013-09-07 18:15 ` manu at gcc dot gnu.org
2014-06-05 19:36 ` law at gcc dot gnu.org
2014-06-06  0:57 ` bugdal at aerifal dot cx
2014-06-06  5:01 ` mpolacek at gcc dot gnu.org
2014-08-03 17:40 ` rose.garcia-eggl2fk at yopmail dot com
2014-08-03 17:58 ` mpolacek at gcc dot gnu.org
2014-08-03 19:33 ` rose.garcia-eggl2fk at yopmail dot com
2015-01-21 18:57 ` rogerdpack at gmail dot com
2015-01-21 19:29 ` mpolacek at gcc dot gnu.org
2015-02-07  0:24 ` safinaskar at mail dot ru
2015-02-07  0:31 ` bugdal at aerifal dot cx
2015-02-07  1:02 ` safinaskar at mail dot ru
2015-03-16  4:32 ` nightstrike at gmail dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).