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


Groups > comp.lang.c > #126872

Re: Opinion sought: Is this strictly conforming C?

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: Opinion sought: Is this strictly conforming C?
Date 2018-02-19 17:11 -0800
Organization None to speak of
Message-ID <ln4lmco479.fsf@kst-u.example.com> (permalink)
References (4 earlier) <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>

Show all headers | View raw


supercat@casperkitty.com writes:
> On Monday, February 19, 2018 at 5:21:34 PM UTC-6, Keith Thompson wrote:
>> supercat@casperkitty.com writes:
>> > On Monday, February 19, 2018 at 3:59:29 PM UTC-6, Keith Thompson wrote:
>> >> In the example in N1570, we have:
>> >>     int a[4][5];
>> >>     a[1][7];
>> >> 
>> >> For a[1][7] to be valid, you'd have to have an array object of at
>> >> least 8 int elements.
>> >> 
>> >> I think (correct me if I'm mistaken) that you're assuming the
>> >> declaration of a creates an object of type int[20].  Can you cite
>> >> wording in the C89/C90 standard that supports this assumption?
>> >
>> > Consider the example from "3.3.2.1 Array subscripting" of C89, which
>> > describes multi-dimensional array subscripting in terms of multiplying
>> > the outer index by the length of the row.  That would suggest a memory
>> > model in which a multi-dimensional array is, and can be treated as, a
>> > single sequence of inner values.
>> 
>> I don't think it suggests it very strongly.
>> 
>> Quoting "ansi.c.txt", a draft of C89 (because it's easier to
>> copy-and-paste from it than from my copy of C90):
>> 
>>     Consider the array object defined by the declaration 
>> 
>>          int x[3][5];
>> 
>>     Here x is a 3x5 array of int s; more precisely, x is an array of
>>     three member objects, each of which is an array of five int s.
>>     In the expression x[i] , which is equivalent to (*(x+(i))) ,
>>     x is first converted to a pointer to the initial array of five
>>     int s.  Then i is adjusted according to the type of x , which
>>     conceptually entails multiplying i by the size of the object to
>>     which the pointer points, namely an array of five int objects.
>>     The results are added and indirection is applied to yield
>>     an array of five int s.  When used in the expression x[i][j] ,
>>     that in turn is converted to a pointer to the first of the
>>     int s, so x[i][j] yields an int.
>
> I read the Standard as suggesting that given T *p, p[i] is equivalent to
> *((T*)((char*)p + i*(int)sizeof(T))), at least in cases where
> "i*(int)sizeof(T)" would fit in an "int".  I see nothing that would
> suggest that p[i] would not behave the same way as the latter expression
> in all such cases.

I have no idea why you added the extraneous (int) cast.

> Further, the C89 Standard does not recognize the possibility that two
> pointers may have the same type and value but not be equivalent.  Does
> anything in the Standard suggest the existences of cases where (p+x)+y
> would be defined, and where computation of x+y neither overflows nor
> "wraps", but where the behavior of p+(x+y) would not be defined equivalent
> to (p+x)+y?  Given int a[4][5], I think it's unambiguous that
> arr[1]+5==arr[2].  Would that not imply that arr[1]+(5+2) is equivalent
> to (arr[1]+5)+2, which is in turn equivalent to arr[2]+2?
>
>> The restrictions on pointer addition (which I cited and you snipped)
>> still apply, even if a given addition might "conceptually" seem valid.
>
> Why describe things in such terms if code can't actually do such
> multiplication and addition itself?

I think the word "conceptually" answers your question.  The phrase
"which conceptually entails multiplying i by the size of the object to
which the pointer points" is intended, I think, as a metaphor to help
the reader understand what's going on.  It's not intended to suggest
that a C multiplication operator is evaluated.

The normative definition of pointer arithmetic does not refer to
multiplication.

The restrictions on pointer arithmetic are not conceptual or implied.
Pointer addition explicitly has undefined behavior if the result doesn't
point to an element of the relevant array object or just past its end.

If a[1][7] is valid in C89, what array object of at least 8 elements
does it refer to?

[snip]

> The authors of C89 weren't trying to mandate absolutely everything that
> should be expected of a useful implementation.  They expected that people
> writing compilers would be familiar with K&R, and would attempt to support
> constructs described therein when practical whether they were ordered to
> or not.  When the easiest way of upholding the Standard wouldn't require a
> compiler to know or care about certain things, and where extant compilers
> simply ignored those things, the authors of the Standard generally didn't
> bother specifying that those things should in fact have no effect.

What normative wording changed between C89 and C11 that made a[1][7]
undefined?

-- 
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