Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #127217

Re: lvalue types

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: lvalue types
Date 2018-03-01 17:37 -0800
Organization None to speak of
Message-ID <ln4llzgsus.fsf@kst-u.example.com> (permalink)
References <2b687d74-f559-43a9-9aec-8a1067467ce2@googlegroups.com>

Show all headers | View raw


supercat@casperkitty.com writes:
> For purposes of 6.5p7, what would be the types of the lvalues on the left
> side of each numbered assignment in the code below [assume store_int is
> called exclusively from test3]?
>
>     struct S {int x[1];};
>     int *p;
>     
>     void test1(struct S *s) { 
>        s->x[0] = 1; // #1
>     }
>
>     void test2(struct S *s)
>     {
>       int *p = s->x;
>       *p = 2; // #2
>     }
>
>     static void store_int(int *p)
>     {
>       *p = 3; // #3 -- when invoked from test3
>     }
>     void test3(struct S *s)
>     {
>       store_int(s->x);
>     }

All three lvalues (s->x[0], *p, and *p) are obviously of type int.

> So far as I can tell, for code to have defined behavior one of the
> following would need to be true for each of the assignments that stores
> a value 1 to 3.
>
> 1. They operate upon an lvalue of type "int", but don't use it to access
>    the stored value of a "struct S" object [even though it sure looks like
>    they do clearly do], or...

Nope.

> 2. An object of aggregate type can be modified using an lvalue of member
>    type, at least in these particular cases, even though that's not one
>    of the possibilities listed in 6.5p7.

Yes, due to an apparent oversight in the standard.

> 3. For purposes of 6.5p7 the left-side operand is an lvalue of type
>    "struct S", even though for purposes of 6.5.3.2 the the type of
>    the lvalues in each case would seem to be "int", or...

Absurd.  The standard is crystal clear that each of the lvalues in
question is of type int.

> 4. The assignments invoke UB because of 6.5p7.

Consistent with an extremely pedantic reading of the standard, but
clearly not the intent.

Let me make the point more succinctly.

    struct s { int m; };
    struct s obj;
    obj.m = 42;

obj.m is an lvalue of type int.  The assignment accesses (specifically
modifies) the object obj.m, which is of type int; that's perfectly fine
under 6.5p7.  It also accesses (i.e., modifies) the object obj, which
is of type struct s.  N1570 6.5p7 says that:

    An object shall have its stored value accessed only by an lvalue
    expression that has one of the following types:

followed by 6 bullet points, none of which allow an object of type
struct s to be accessed by an lvalue of type int.

It's obvious that the assignment is meant to be well defined.  I'd say
this is a simple oversight in the wording of the standard (unless
someone can come up with another interpretation).  I don't know whether
anyone else has noticed this before.  If you're the first,
congratulations.

This also applies to array elements and union members.

We've discussed this before quite recently.

[snip]

I suggest that 6.5p7 should be updated to state that, in addition to the
6 bulleted possibilities, a valid access of a subobject is a valid
access of the containing object.  I don't believe any other changes are
needed.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

lvalue types supercat@casperkitty.com - 2018-03-01 16:38 -0800
  Re: lvalue types Steven Petruzzellis <frelwizzen@gmail.com> - 2018-03-01 17:17 -0800
  Re: lvalue types Keith Thompson <kst-u@mib.org> - 2018-03-01 17:37 -0800
    Re: lvalue types Steven Petruzzellis <frelwizzen@gmail.com> - 2018-03-01 22:00 -0800
    Re: lvalue types Steven Petruzzellis <frelwizzen@gmail.com> - 2018-03-01 23:49 -0800
      Re: lvalue types asetofsymbols@gmail.com - 2018-03-05 06:56 -0800
    Re: lvalue types supercat@casperkitty.com - 2018-03-02 00:22 -0800
      Re: lvalue types Steven Petruzzellis <frelwizzen@gmail.com> - 2018-03-02 01:23 -0800
      Re: lvalue types Steven Petruzzellis <frelwizzen@gmail.com> - 2018-03-02 05:04 -0800
        Re: lvalue types Steven Petruzzellis <frelwizzen@gmail.com> - 2018-03-02 06:06 -0800
          Re: lvalue types Steven Petruzzellis <frelwizzen@gmail.com> - 2018-03-02 08:12 -0800
      Re: lvalue types Keith Thompson <kst-u@mib.org> - 2018-03-02 09:50 -0800
        Re: lvalue types supercat@casperkitty.com - 2018-03-02 11:33 -0800
          Re: lvalue types Keith Thompson <kst-u@mib.org> - 2018-03-02 11:45 -0800
            Re: lvalue types supercat@casperkitty.com - 2018-03-02 16:28 -0800
              Re: lvalue types Keith Thompson <kst-u@mib.org> - 2018-03-02 16:54 -0800
                Re: lvalue types supercat@casperkitty.com - 2018-03-02 17:50 -0800
                Re: lvalue types Keith Thompson <kst-u@mib.org> - 2018-03-02 19:43 -0800
                Re: lvalue types Tim Rentsch <txr@alumni.caltech.edu> - 2018-03-02 22:51 -0800
                Re: lvalue types Steven Petruzzellis <frelwizzen@gmail.com> - 2018-03-02 23:07 -0800
                Re: lvalue types Steven Petruzzellis <frelwizzen@gmail.com> - 2018-03-03 01:44 -0800
                Re: lvalue types supercat@casperkitty.com - 2018-03-03 13:01 -0800
          Re: lvalue types Steven Petruzzellis <frelwizzen@gmail.com> - 2018-03-02 22:21 -0800
    Re: lvalue types supercat@casperkitty.com - 2018-03-02 09:24 -0800
    Re: lvalue types Tim Rentsch <txr@alumni.caltech.edu> - 2018-03-03 00:58 -0800
      Re: lvalue types Steven Petruzzellis <frelwizzen@gmail.com> - 2018-03-03 01:13 -0800
      Re: lvalue types Keith Thompson <kst-u@mib.org> - 2018-03-03 13:01 -0800
        Re: lvalue types supercat <flatfinger@casperkitty.com> - 2018-03-03 17:43 -0800
        Re: lvalue types Tim Rentsch <txr@alumni.caltech.edu> - 2018-03-04 02:22 -0800
          Re: lvalue types supercat@casperkitty.com - 2018-03-04 13:03 -0800
          Re: lvalue types Keith Thompson <kst-u@mib.org> - 2018-03-04 13:03 -0800
            Re: lvalue types bartc <bc@freeuk.com> - 2018-03-04 22:32 +0000
            Re: lvalue types supercat@casperkitty.com - 2018-03-05 09:45 -0800
      Re: lvalue types supercat@casperkitty.com - 2018-03-03 14:19 -0800
  Re: lvalue types Steven Petruzzellis <frelwizzen@gmail.com> - 2018-03-01 18:03 -0800

csiph-web