gpsd-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Git][gpsd/gpsd][master] 2 commits: serial.c: Fix some warnings.


From: Gary E. Miller
Subject: Re: [Git][gpsd/gpsd][master] 2 commits: serial.c: Fix some warnings.
Date: Fri, 10 Sep 2021 14:34:54 -0700

Yo Fred!

On Fri, 10 Sep 2021 14:10:47 -0700 (PDT)
Fred Wright <fw@fwright.net> wrote:

> On Fri, 10 Sep 2021, Gary E. Miller wrote:
> 
> > 699108bc by Fred Wright at 2021-09-10T00:37:27-07:00
> > gpsd.c: Fix an initializer warning.
> >
> > Since devices is an array of structs, it needs extra braces in the
> > initializer to refer to a single element.
> >
> > TESTED:
> > Warning is gone.
> >
> >
> > Yes, but the intent was to initialize the entire array, not just
> > a single element.  
> 
> The C standard states that if any element of a composite variable is 
> initialized, then all unspecified elements are initialized to 0.
> That's especially useful for auto variables.

Yes.

> > -static struct gps_device_t devices[MAX_DEVICES] = {0};
> > +static struct gps_device_t devices[MAX_DEVICES] = {{{0}}};  
> 
> The first form is equating an entire array of structs to a single
> scalar 0, which is technically a type mismatch, even if some
> compilers accept it. The second form initializes the first struct
> element of the first array element to 0, and lets the compiler
> implicitly initialize the rest to 0.

IMHO, rejecting the first one violates the C standard.
The second one makes no sense to me.  Except, I can't find either in
the C standard...

> > Since static forces devices to be all zeros, best to just do this:
> >
> > static struct gps_device_t devices[MAX_DEVICES];  
> 
> Also OK (as mentioned in the preexisting comment)

And since we agree, I'll leave it that way.  The other was just
belt and suspenders.

> but adding the
> braces was more consistent with what was already there.

But makes no sens to me...  No mater, not needed, so gone.

> > What compiler?  
> 
> Clang, which tends to be pickier about such things.

Have you read what Linus has to say about clang?  Not flattering.

> > My understanding of the C standard is that {0} will initialize
> > anything to all zeros.  That is what gcc does.  
> 
> See above.

So, I looked in the C11 standard.  Neither {)} nor {{{0}} is there. 

static is in the standard here: "6.7.8 Initialization".  And it
specifies setting the static variable to zeros, in a round about
way.  So static will do. With the comment as to why it is static.

I guess I confused a gcc-ism with the standard, again.

RGDS
GARY
---------------------------------------------------------------------------
Gary E. Miller Rellim 109 NW Wilmington Ave., Suite E, Bend, OR 97703
        gem@rellim.com  Tel:+1 541 382 8588

            Veritas liberabit vos. -- Quid est veritas?
    "If you can't measure it, you can't improve it." - Lord Kelvin

Attachment: pgp5PT4PoeN1o.pgp
Description: OpenPGP digital signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]