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


Groups > comp.lang.c > #18843

Re: i think i found

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: i think i found
Date 2012-03-17 18:02 -0700
Organization None to speak of
Message-ID <lnfwd6raah.fsf@nuthaus.mib.org> (permalink)
References <jje9kd$d14$1@reader1.panix.com> <lnk42ocf5l.fsf@nuthaus.mib.org> <jjr9th$f7d$1@reader1.panix.com> <kfn7gyll5m7.fsf@x-alumni2.alumni.caltech.edu> <jk2i03$gub$1@reader1.panix.com>

Show all headers | View raw


jgk@panix.com (Joe keane) writes:
> In article <kfn7gyll5m7.fsf@x-alumni2.alumni.caltech.edu>,
> Tim Rentsch  <txr@alumni.caltech.edu> wrote:
>>If you care, an assignment (or initialization) like
>>
>>    int *p = (int*) &b;
>>
>>is more likely to work as you expect (ie, and be defined
>>behavior) for the "cross-subarray-bounds" indexing like
>>that shown in the example.
>
> I debated how to write this.
>
> Is it possible the following are different:
>
>     p = (int *) &b;
>     p = (int *) &b[0];
>     p = /int */ &b[0][0];

The definition of b, which you haven't quoted, is

    int b[4][5];

You wanted parentheses on that third line.

> Maybe some work and others don't?

I believe all of these "work" in the sense that the conversion yields a
pointer to the int object b[0][0] (though I'm not 100% certain that
that's guaranteed).  It doesn't follow that you can safely increment
that pointer to point to elements outside b[0].

> It seems like we're just guessing here.
>
> I try to follow the standard, even the parts that contradict the other
> parts.

I'm not aware of any contradiction in this area.

> I've been programming C for a long time, and i just 'knew' this is
> perfectly valid C (for some reason), so i was a bit shocked.
>
> I would just say, for 'int *' not 'int (*)[N]', we can increment it,
> valid as long as you're within the *main* array, that it is whatever it
> is declared as, global, or on the stack, or from malloc, and not just in
> the first 'row', it is contiguous in all dimensions, and we should have
> &a[1][0] == &a[0][5], and it is nonsensical for them to work different.
>
> That seems like a basic property, first, they are contiguous, and second,
> you can actually use that in your code.
>
> If the standard doesn't say so, or says it is and it isn't at the same
> time, i think it's defective.

Given
    int b[4][5];
the standard says that the elements of b are adjacent.  It also says
that the expression
    b[0][7]
has undefined behavior.  That doesn't mean that it must fail; it simply
means that the standard doesn't define its behavior.  It certainly *can*
do what you expect it to do.  The point is that the standard gives
compilers latitude to do something else.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
    Will write code for food.
"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

i think i found jgk@panix.com (Joe keane) - 2012-03-10 01:09 +0000
  Re: i think i found Ian Collins <ian-news@hotmail.com> - 2012-03-10 14:23 +1300
  Re: i think i found nick_keighley_nospam@hotmail.com - 2012-03-10 03:13 -0800
  Re: i think i found Barry Schwarz <schwarz45@yahoo.com> - 2012-03-10 09:17 -0800
    Re: i think i found pete <pfiland@mindspring.com> - 2012-03-10 19:22 -0500
      Re: i think i found James Kuyper <jameskuyper@verizon.net> - 2012-03-10 20:14 -0500
        Re: i think i found pete <pfiland@mindspring.com> - 2012-03-10 21:59 -0500
      Re: i think i found jgk@panix.com (Joe keane) - 2012-03-12 17:29 +0000
    Re: i think i found jgk@panix.com (Joe keane) - 2012-03-11 19:35 +0000
      Re: i think i found Keith Thompson <kst-u@mib.org> - 2012-03-11 13:06 -0700
        Re: i think i found jgk@panix.com (Joe keane) - 2012-03-13 20:27 +0000
          Re: i think i found Keith Thompson <kst-u@mib.org> - 2012-03-13 15:25 -0700
            Re: i think i found jgk@panix.com (Joe keane) - 2012-03-14 23:34 +0000
              Re: i think i found Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-03-14 20:38 -0400
              Re: i think i found Tim Rentsch <txr@alumni.caltech.edu> - 2012-03-15 18:03 -0700
                Re: i think i found jgk@panix.com (Joe keane) - 2012-03-17 17:34 +0000
                Re: i think i found Keith Thompson <kst-u@mib.org> - 2012-03-17 18:02 -0700
                Re: i think i found jgk@panix.com (Joe keane) - 2012-03-19 15:41 +0000
                Re: i think i found Tim Rentsch <txr@alumni.caltech.edu> - 2012-03-21 05:52 -0700

csiph-web