9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: Dan Cross <crossd@gmail.com>
To: Fans of the OS Plan 9 from Bell Labs <9fans@9fans.net>
Subject: Re: [9fans] Don't Plan 9 C compiler initialize the rest of member of a?struct?
Date: Mon,  1 Apr 2019 21:20:43 -0400	[thread overview]
Message-ID: <CAEoi9W5R0iLDoYmL1uC7Y=RRtRxmyVPnWW_OwB25NhsUvj70Mg@mail.gmail.com> (raw)
In-Reply-To: <20190402003532.GA55021@wopr>

[-- Attachment #1: Type: text/plain, Size: 2282 bytes --]

On Mon, Apr 1, 2019 at 8:36 PM Kurt H Maier <khm@sciops.net> wrote:

> On Mon, Apr 01, 2019 at 08:26:30PM -0400, Jeremy O'Brien wrote:
> > On Mon, Apr 1, 2019, at 11:33, Kyohei Kadota wrote:
> > > Hi, 9fans. I use 9legacy.
> > >
> > > About below program, I expected that flags field will initialize to
> > > zero but the value of flags was a garbage, ex, "f8f7".
> > > Is this expected?
> > >
> > > ```
> > > #include <stdio.h>
> > >
> > > struct option {
> > >     int n;
> > >     char *s;
> > >     int flags;
> > > };
> > >
> > > int
> > > main(void)
> > > {
> > >     struct option opt = {1, "test"};
> > >     printf("%d %s %x\n", opt.n, opt.s, opt.flags);
> > >     return 0;
> > > }
> > > ```
> > >
> > >
> >
> > According to C99: "If an object that has automatic storage duration is
> not initialized explicitly, its value is indeterminate."
> >
> > Stack variable == automatic storage duration. This appears to be correct
> behavior to me.
> >
>
> Can anyone provide the patches 9legacy uses to implement C99 compliance?


There were actually quite a few of them, mostly done by Geoff Collyer.  The
compiler sources list contains a list of desiderata in a file called `c99`;
of course, the plan9 compilers aren't completely compliant (they weren't
trying to be). Incidentally this file has been carried forward into, for
example, /sys/src/cmd/cc/c99 in the 9front distribution (and other plan9
derivatives).

In the present case, this appears to be a compiler bug. The aforementioned
reference to n1548 sec 6.7.9 para 10 is incorrect in that there _is_ an
explicit initializer here. The relevant text in the standard is sec 6.7.9
pp 16-21, which specifies that in the event that an explicit initializer
does not completely cover (in a topological sense) the thing it is
initializing, then the elements not covered shall be initialized as if they
had _static_ storage duration; that is, they should be zeroed.

Now as I said, the Plan 9 C compilers aren't explicit C99 compliant. But
given that the `c99` file describes things related to initializer lists as
being unneeded because they were already implemented, one may assume it was
believed that this was covered by c99 behavior. It isn't.

        - Dan C.

[-- Attachment #2: Type: text/html, Size: 2957 bytes --]

  reply	other threads:[~2019-04-02  1:20 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-04-01 15:32 [9fans] Don't Plan 9 C compiler initialize the rest of member of a struct? Kyohei Kadota
2019-04-01 23:49 ` Skip Tavakkolian
2019-04-02 14:44   ` Kyohei Kadota
2019-04-02  0:26 ` Jeremy O'Brien
2019-04-02  0:35   ` Charles Forsyth
2019-04-02  0:35   ` [9fans] Don't Plan 9 C compiler initialize the rest of member of a?struct? Kurt H Maier
2019-04-02  1:20     ` Dan Cross [this message]
2019-04-02  2:22       ` Kurt H Maier
2019-04-02  3:06         ` Skip Tavakkolian
2019-04-02  8:02           ` David du Colombier
2019-04-02 10:38         ` Charles Forsyth
2019-04-02 16:25           ` Anthony Martin
2019-04-02 16:37             ` Charles Forsyth
2019-04-02 14:52   ` [9fans] Don't Plan 9 C compiler initialize the rest of member of a struct? Kyohei Kadota
2019-04-02 15:02     ` Skip Tavakkolian
2019-04-02 15:14       ` Skip Tavakkolian
2019-04-02 16:18         ` Devon H. O'Dell
2019-04-02 19:57           ` Charles Forsyth
2019-04-02 15:04     ` Charles Forsyth
2019-04-02 15:16       ` Kyohei Kadota

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAEoi9W5R0iLDoYmL1uC7Y=RRtRxmyVPnWW_OwB25NhsUvj70Mg@mail.gmail.com' \
    --to=crossd@gmail.com \
    --cc=9fans@9fans.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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 NNTP newsgroup(s).