On Tue, 2016-09-27 at 15:05 -0400, Robbie Harwood wrote:
Nathaniel McCallum <npmccallum(a)redhat.com> writes:
> On Tue, 2016-09-27 at 13:26 -0400, Robbie Harwood wrote:
> > Nathaniel McCallum <npmccallum(a)redhat.com> writes:
> > > On Tue, 2016-09-27 at 11:08 -0400, Simo Sorce wrote:
> > > > On Tue, 2016-09-27 at 09:13 -0400, Nathaniel McCallum wrote:
> > > >
> > > > > Why doesn't "gssx_cred remote = {};" work?
> > > >
> > > >
> > > > I know it should work, and do not know why it didn't, but I
> > > > pushed
> > > > this patch anyway, I can put in something better later as
> > > > long as
> > > > clang does not complain.
> > >
> > >
> > > I suspect that clang is complaining because zero isn't the
> > > right
> > > value for the first field of gssx_cred. Simply removing that
> > > one
> > > byte should be sufficient to make clang happy.
> > >
> > > What is the actual complaint?
> >
> >
> > Clang says this:
> >
> > > src/mechglue/gpp_creds.c:186:29: warning: suggest braces around
> > > initialization of subobject [-Wmissing-braces]
> > > gssx_cred remote = {0};
> > > ^
> > > {}
> > > src/mechglue/gpp_creds.c:186:29: warning: suggest braces around
> > > initialization of subobject [-Wmissing-braces]
> > > gssx_cred remote = {0};
> > > ^
> > > {}
> > > 2 warnings generated.
> >
> >
> > and it's warning twice because what it wants to see is `gssx_cred
> > remote = {{{0}}};`. Given the choice, I prefer the memset() to
> > this
> > notation.
>
>
> Did you try taking the zero out?
>
> I've seen this idiom before in our group and it (almost) never
> makes
> sense. {0} attempts to initialize the first scalar field to zero.
> It is
> functionally identical to {} when the first field of the struct is
> a
> scalar. But it often isn't. When the first field is, say, a struct,
> {0}
> is a syntax error.
>
> C99 defines that when using an initializer, any unspecified fields
> are
> initialized to zero. Thus, {} is perfectly valid for all cases when
> initializing a struct to zero.
Wasn't aware of that. Good to know, thanks.
> Using an empty initializer should be preferred because the compiler
> can
> simply align all the zeroed stack variables and do a single zero
> for
> all of them. Using memset() is harder to optimize this way.
On my machine (x86_64 clang-3.6.2), the two compile to the same code,
and that code is what memset() (i.e., bzero()) also compiles to.
It doesn't really matter to me which we use; I went with the version
I
immediately understood.
It probably isn't worth changing now. I think it is just best to use
the syntax I provided in the future since it is the most readable.
There are some cases (like VLAs) where this syntax doesn't work, but I
think it should be preferred.
Nathaniel