On Wed, May 12, 2021 at 12:16:54PM +0200, Bruno Haible wrote:
> > > dfa.c:1627:19: warning: suggest braces around initialization of subobject
> > >       [-Wmissing-braces]
> > >   mbstate_t s = { 0 };
> > >                   ^
> > >                   {}
> > > 1 warning generated.
> 
> { 0 } is the only portable initializer for an mbstate_t. If we were to use
> another initializer, it would make assumptions about the structure of an
> mbstate_t. That is, #ifdefs.

Unfortunately, although C declares { 0 } to be the universal
initializer, there are some compilers (such as older clang versions,
like the one on MacOS) that incorrectly warn for it in certain cases
(gcc used to have problems with it, too).

Using 'mbstate_t s = { };' would work on gcc and clang (even those
older versions that warn on { 0 }), but is not portable C.  Maybe the
compromise is to define a macro that expands to empty on gcc/clang and
to 0 otherwise, as in 'mbstate_t s = { ZERO_INIT };' ?

> 
> Another option was to use to portable initializer, but use
> memset (&s, '\0', sizeof (s)) to initialize the variable.
> This is ugly BSD style of the 1980ies, which we have abandoned
> more than 10 years ago.

Yet another portable solution is:

static mbstate_t s1;
mbstate_t s = s1;

also with its own form of ugliness.

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org


Reply via email to