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


Groups > comp.lang.c > #126971

Re: Opinion sought: Is this strictly conforming C?

Path csiph.com!eternal-september.org!feeder.eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: Opinion sought: Is this strictly conforming C?
Date Wed, 21 Feb 2018 17:46:22 -0800
Organization None to speak of
Lines 109
Message-ID <lninaplrtd.fsf@kst-u.example.com> (permalink)
References <e054cddb-6cb6-46d3-805a-db0733421bed@googlegroups.com> <29e38fdf-86ef-4935-891a-b2683ee99b88@googlegroups.com> <kfnfu5wwwun.fsf@x-alumni2.alumni.caltech.edu> <42bf72ce-89a8-405e-9155-9fbebe4b6d31@googlegroups.com> <lnd110od3i.fsf@kst-u.example.com> <fd094d02-819b-42f5-9f97-89587ef3bf00@googlegroups.com> <ln8tboo9an.fsf@kst-u.example.com> <b250f55e-7ce4-469c-961c-98d549f83abd@googlegroups.com> <p6glj4$jro$1@dont-email.me> <f5234659-1e2c-4e92-b16f-3068786a5f30@googlegroups.com> <lnsh9vmv35.fsf@kst-u.example.com> <2dc98694-34c7-4c1a-ac2d-8b82ca95369e@googlegroups.com> <BG5jC.57834$9z2.31034@fx43.iad> <ec78cf65-4533-46eb-96cf-d056c989765c@googlegroups.com> <p6jqts$1fak$1@gioia.aioe.org> <lnzi42kz9r.fsf@kst-u.example.com> <95ef2dbd-24aa-415b-ada7-6e933b27cf2b@googlegroups.com> <lnr2pekq2p.fsf@kst-u.example.com> <5b4dbe5b-b7b0-48f4-8511-d443695f0e5b@googlegroups.com> <lnmv01lx50.fsf@kst-u.example.com> <2ea2a3c6-3578-4d3c-b366-f19d4741245e@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=us-ascii
Injection-Info reader02.eternal-september.org; posting-host="ab1a41b8f31428ef69f5dd75d8b89c54"; logging-data="25523"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+4UXhy2yiAqAC7pUbi6P2j"
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux)
Cancel-Lock sha1:7wNahiJ/ifwHoM7nyLJVfZ7/+K0= sha1:3nJeXXpD8GY9lmd4c2dturpOJ8Y=
Xref csiph.com comp.lang.c:126971

Show key headers only | View raw


supercat@casperkitty.com writes:
> On Wednesday, February 21, 2018 at 5:51:37 PM UTC-6, Keith Thompson wrote:
>> supercat@casperkitty.com writes:
>> > The code above would create an array object sized to hold a total of 25
>> > values of type "int".  Assume "int" is 4 bytes; the size of the object
>> > would be 100 bytes.
>> 
>> Yes, it would create an array object that contains 25 int objects, but
>> not an array object of type int[25].  Each of the 5 elements of a is of
>> type int[5].
>
> There would be a 100-byte array object.

Yes, you already said that, and I already agreed.

>> > According to C89 3.3.6, the subexpression a[1] would multiply the value in
>> > the brackets (i.e. 1) by the size of *a [i.e. 20], and add that result to
>> > the address of the aforementioned array object.  Then applying [7] to that
>> > would multiply the value 7 by the size of **a [i.e. 4] and that to the
>> > just-computed address, yielding an address 48 bytes past the start of the
>> > 100-byte array object.
>> 
>> Yes, the C89 definition of pointer arithmetic refers to multiplication.
>> (In my opinion the current definition, which does not, is better.)
>
> So you acknowledge that there was a change.

Of course, the wording was changed.  (In my opinion it was an
improvement; C89's reference to multiplication was imprecise.)

I'm not aware that the changed wording changed the semantics, but I
haven't studied it closely enough to be sure.  Do you think the
semantics changed?

>> C89 3.3.6:
>>     When an expression that has integral type is added to
>>     or subtracted from a pointer, the integral value is first
>>     multiplied by the size of the object pointed to.  The result
>>     has the type of the pointer operand.  If the pointer operand
>>     points to a member of an array object, and the array object is
>>     large enough, the result points to a member of the same array
>>     object, appropriately offset from the original member.
>> 
>> For a[1][7] to work the way you want it to, there would have be an array
>> object of type int[N], with N > 7.  (The most straightforward way to
>> have this would be to treat all of a as an array object of type
>> int[25].)  The elements of a are not int objects; they're array objects
>> of type int[5].
>
> The Standard could have been clearer if it didn't overload the term
> "object" so much.  The Standard clearly intends that the code:
>
>     int arr[5], p=a+5, q=p-1; 
>
> should have defined behavior even though "p" doesn't point to any object;
> thus "the size of the object pointed to" presumably means "the size of
> the lvalue yielded by dereferencing a pointer of that type".

C11 (N1570 6.5.6) is clearer on this point, though the wording IMHO
could still be fixed up a bit.  It discusses pointer arithmetic where
the pointer operand points just past the end of an array.

> I would suggest that the Standard also clearly intends that the code:
>
>     int *p = malloc(3 * sizeof (int));
>     if (p) p[2] = 5;
>
> should have defined behavior even though there is no int[].  The malloc()
> function would be pretty well useless if its return pointer couldn't be
> treated, for purposes of pointer arithmetic, as though it pointed to an
> array object.

I think the effective type rules cover this.  (C89/C90 didn't discuss
effective types.)

>> I didn't ask whether a such an implementation would be useful.  I asked
>> what (normative) clause of C90 it would violate.
>
> The one which describes how pointer arithmetic works.  There exists an
> array object.  The lvalue a[1] points to four bytes within it.

lvalues don't point.  The lvalue a[1] is an expression that designates
an object of type int[5].  Since it's an array expression, it's
converted to a pointer expression which is no longer an lvalue.  The
value of that pointer expression is the address of an int object.

>                                                                 Adding
> 28 to the address of a[1] would yield the address of different four
> bytes within that same array object.

What array object are referring to?  You seem to be assuming the
existence of an object of type int[25], but you haven't demonstrated
where the C89 standard says that such an object exists, or that a can be
accessed as if it were such an object.

"If the pointer operand points to a member of an array object"

The array object in question is a[1].  The member in question is
a[1][0].

"and the array object is large enough,"

The array object a[1] has only 5 members, so it isn't large enough.

-- 
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 | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Opinion sought: Is this strictly conforming C? lee@ansync.com - 2018-02-15 17:20 -0800
  Re: Opinion sought: Is this strictly conforming C? James Kuyper <jameskuyper@verizon.net> - 2018-02-15 22:15 -0500
    Re: Opinion sought: Is this strictly conforming C? supercat@casperkitty.com - 2018-02-16 08:44 -0800
      Re: Opinion sought: Is this strictly conforming C? jameskuyper@verizon.net - 2018-02-16 11:47 -0800
        Re: Opinion sought: Is this strictly conforming C? supercat@casperkitty.com - 2018-02-16 13:00 -0800
      Re: Opinion sought: Is this strictly conforming C? Tim Rentsch <txr@alumni.caltech.edu> - 2018-02-19 12:25 -0800
        Re: Opinion sought: Is this strictly conforming C? supercat@casperkitty.com - 2018-02-19 13:21 -0800
          Re: Opinion sought: Is this strictly conforming C? Randy Howard <rhoward.mx@EverybodyUsesIt.com> - 2018-02-19 15:26 -0600
          Re: Opinion sought: Is this strictly conforming C? Keith Thompson <kst-u@mib.org> - 2018-02-19 13:59 -0800
            Re: Opinion sought: Is this strictly conforming C? supercat@casperkitty.com - 2018-02-19 14:38 -0800
              Re: Opinion sought: Is this strictly conforming C? Keith Thompson <kst-u@mib.org> - 2018-02-19 15:21 -0800
                Re: Opinion sought: Is this strictly conforming C? supercat@casperkitty.com - 2018-02-19 15:59 -0800
                Re: Opinion sought: Is this strictly conforming C? Keith Thompson <kst-u@mib.org> - 2018-02-19 17:11 -0800
                Re: Opinion sought: Is this strictly conforming C? David Brown <david.brown@hesbynett.no> - 2018-02-20 09:17 +0100
                Re: Opinion sought: Is this strictly conforming C? supercat@casperkitty.com - 2018-02-20 07:58 -0800
                Re: Opinion sought: Is this strictly conforming C? David Brown <david.brown@hesbynett.no> - 2018-02-20 18:21 +0100
                Re: Opinion sought: Is this strictly conforming C? Keith Thompson <kst-u@mib.org> - 2018-02-20 09:25 -0800
                Re: Opinion sought: Is this strictly conforming C? supercat@casperkitty.com - 2018-02-20 11:52 -0800
                Re: Opinion sought: Is this strictly conforming C? Richard Damon <Richard@Damon-Family.org> - 2018-02-20 22:27 -0500
                Re: Opinion sought: Is this strictly conforming C? supercat@casperkitty.com - 2018-02-20 23:07 -0800
                Re: Opinion sought: Is this strictly conforming C? Manfred <noname@invalid.add> - 2018-02-21 14:07 +0100
                Re: Opinion sought: Is this strictly conforming C? supercat@casperkitty.com - 2018-02-21 09:02 -0800
                Re: Opinion sought: Is this strictly conforming C? Keith Thompson <kst-u@mib.org> - 2018-02-21 09:50 -0800
                Re: Opinion sought: Is this strictly conforming C? supercat@casperkitty.com - 2018-02-21 12:46 -0800
                Re: Opinion sought: Is this strictly conforming C? Keith Thompson <kst-u@mib.org> - 2018-02-21 13:09 -0800
                Re: Opinion sought: Is this strictly conforming C? supercat@casperkitty.com - 2018-02-21 15:15 -0800
                Re: Opinion sought: Is this strictly conforming C? Keith Thompson <kst-u@mib.org> - 2018-02-21 15:51 -0800
                Re: Opinion sought: Is this strictly conforming C? supercat@casperkitty.com - 2018-02-21 17:06 -0800
                Re: Opinion sought: Is this strictly conforming C? Keith Thompson <kst-u@mib.org> - 2018-02-21 17:46 -0800
                Re: Opinion sought: Is this strictly conforming C? supercat@casperkitty.com - 2018-02-22 00:18 -0800
                Re: Opinion sought: Is this strictly conforming C? Tim Rentsch <txr@alumni.caltech.edu> - 2018-02-21 22:14 -0800
                Re: Opinion sought: Is this strictly conforming C? Tim Rentsch <txr@alumni.caltech.edu> - 2018-02-21 11:39 -0800
                Re: Opinion sought: Is this strictly conforming C? supercat@casperkitty.com - 2018-02-21 14:59 -0800
                Re: Opinion sought: Is this strictly conforming C? Richard Damon <Richard@Damon-Family.org> - 2018-02-21 22:14 -0500
                Re: Opinion sought: Is this strictly conforming C? supercat@casperkitty.com - 2018-02-22 00:50 -0800
                Re: Opinion sought: Is this strictly conforming C? Richard Damon <Richard@Damon-Family.org> - 2018-02-22 07:22 -0500
                Re: Opinion sought: Is this strictly conforming C? supercat@casperkitty.com - 2018-02-22 12:40 -0800
                Re: Opinion sought: Is this strictly conforming C? Keith Thompson <kst-u@mib.org> - 2018-02-22 13:00 -0800
                Re: Opinion sought: Is this strictly conforming C? supercat@casperkitty.com - 2018-02-22 13:48 -0800
                Re: Opinion sought: Is this strictly conforming C? scott@slp53.sl.home (Scott Lurndal) - 2018-02-22 22:14 +0000
                Re: Opinion sought: Is this strictly conforming C? Tim Rentsch <txr@alumni.caltech.edu> - 2018-02-21 23:05 -0800
          Re: Opinion sought: Is this strictly conforming C? Tim Rentsch <txr@alumni.caltech.edu> - 2018-02-21 23:39 -0800
            Re: Opinion sought: Is this strictly conforming C? Keith Thompson <kst-u@mib.org> - 2018-02-22 10:39 -0800
              Re: Opinion sought: Is this strictly conforming C? supercat@casperkitty.com - 2018-02-22 13:20 -0800
              Re: Opinion sought: Is this strictly conforming C? asetofsymbols@gmail.com - 2018-02-23 00:30 -0800
    Re: Opinion sought: Is this strictly conforming C? Manfred <noname@invalid.add> - 2018-02-16 20:05 +0100
      Re: Opinion sought: Is this strictly conforming C? lee@ansync.com - 2018-02-16 11:14 -0800
      Re: Opinion sought: Is this strictly conforming C? Tim Rentsch <txr@alumni.caltech.edu> - 2018-02-19 11:30 -0800
      Re: Opinion sought: Is this strictly conforming C? Steve Carroll <frelwizzen@gmail.com> - 2018-03-01 09:01 -0800
  Re: Opinion sought: Is this strictly conforming C? Tim Rentsch <txr@alumni.caltech.edu> - 2018-02-19 11:08 -0800
    Re: Opinion sought: Is this strictly conforming C? supercat@casperkitty.com - 2018-02-19 17:16 -0800
      Re: Opinion sought: Is this strictly conforming C? Tim Rentsch <txr@alumni.caltech.edu> - 2018-02-21 23:16 -0800
        Re: Opinion sought: Is this strictly conforming C? supercat@casperkitty.com - 2018-02-22 00:25 -0800
          Re: Opinion sought: Is this strictly conforming C? Richard Damon <Richard@Damon-Family.org> - 2018-02-22 07:41 -0500
          Re: Opinion sought: Is this strictly conforming C? Tim Rentsch <txr@alumni.caltech.edu> - 2018-02-24 13:24 -0800
            Re: Opinion sought: Is this strictly conforming C? supercat@casperkitty.com - 2018-02-25 12:07 -0800
              Re: Opinion sought: Is this strictly conforming C? Richard Damon <Richard@Damon-Family.org> - 2018-02-25 18:34 -0500
                Re: Opinion sought: Is this strictly conforming C? supercat@casperkitty.com - 2018-02-26 08:21 -0800
                Re: Opinion sought: Is this strictly conforming C? Richard Damon <Richard@Damon-Family.org> - 2018-02-26 22:22 -0500
                Re: Opinion sought: Is this strictly conforming C? David Brown <david.brown@hesbynett.no> - 2018-02-27 09:16 +0100
                Re: Opinion sought: Is this strictly conforming C? supercat@casperkitty.com - 2018-02-27 08:48 -0800
                Re: Opinion sought: Is this strictly conforming C? David Brown <david.brown@hesbynett.no> - 2018-02-28 10:24 +0100
                Re: Opinion sought: Is this strictly conforming C? supercat@casperkitty.com - 2018-02-28 09:03 -0800
                Re: Opinion sought: Is this strictly conforming C? David Brown <david.brown@hesbynett.no> - 2018-02-28 22:31 +0100
                Re: Opinion sought: Is this strictly conforming C? supercat@casperkitty.com - 2018-02-28 15:19 -0800
                Re: Opinion sought: Is this strictly conforming C? David Brown <david.brown@hesbynett.no> - 2018-03-01 12:37 +0100
                Re: Opinion sought: Is this strictly conforming C? supercat@casperkitty.com - 2018-03-01 11:03 -0800
                Re: Opinion sought: Is this strictly conforming C? Keith Thompson <kst-u@mib.org> - 2018-03-01 11:22 -0800
                Re: Opinion sought: Is this strictly conforming C? Steven Petruzzellis <frelwizzen@gmail.com> - 2018-03-01 11:48 -0800
                Re: Opinion sought: Is this strictly conforming C? Steven Petruzzellis <frelwizzen@gmail.com> - 2018-03-01 15:11 -0800
                Re: Opinion sought: Is this strictly conforming C? Steven Petruzzellis <frelwizzen@gmail.com> - 2018-03-01 16:14 -0800
                Re: Opinion sought: Is this strictly conforming C? Steven Petruzzellis <frelwizzen@gmail.com> - 2018-03-01 19:53 -0800
                Re: Opinion sought: Is this strictly conforming C? Steven Petruzzellis <frelwizzen@gmail.com> - 2018-03-01 21:27 -0800
                Re: Opinion sought: Is this strictly conforming C? Steven Petruzzellis <frelwizzen@gmail.com> - 2018-03-02 00:52 -0800
                Re: Opinion sought: Is this strictly conforming C? Steven Petruzzellis <frelwizzen@gmail.com> - 2018-03-02 01:54 -0800
                Re: Opinion sought: Is this strictly conforming C? Steven Petruzzellis <frelwizzen@gmail.com> - 2018-03-03 06:11 -0800
                Re: Opinion sought: Is this strictly conforming C? supercat@casperkitty.com - 2018-03-01 12:31 -0800
                Re: Opinion sought: Is this strictly conforming C? Keith Thompson <kst-u@mib.org> - 2018-03-01 13:15 -0800
                Re: Opinion sought: Is this strictly conforming C? Steven Petruzzellis <frelwizzen@gmail.com> - 2018-03-01 14:55 -0800
                Re: Opinion sought: Is this strictly conforming C? Tim Rentsch <txr@alumni.caltech.edu> - 2018-03-03 02:19 -0800
                Re: Opinion sought: Is this strictly conforming C? Steven Petruzzellis <frelwizzen@gmail.com> - 2018-03-03 03:03 -0800
              Re: Opinion sought: Is this strictly conforming C? Keith Thompson <kst-u@mib.org> - 2018-02-25 15:36 -0800
                Re: Opinion sought: Is this strictly conforming C? supercat@casperkitty.com - 2018-02-26 10:17 -0800

csiph-web