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


Groups > comp.lang.c > #18863

Re: i think i found

From jgk@panix.com (Joe keane)
Newsgroups comp.lang.c
Subject Re: i think i found
Date 2012-03-19 15:41 +0000
Organization Public Access Networks Corp.
Message-ID <jk7k3h$o9$1@reader1.panix.com> (permalink)
References <jje9kd$d14$1@reader1.panix.com> <kfn7gyll5m7.fsf@x-alumni2.alumni.caltech.edu> <jk2i03$gub$1@reader1.panix.com> <lnfwd6raah.fsf@nuthaus.mib.org>

Show all headers | View raw


In article <lnfwd6raah.fsf@nuthaus.mib.org>,
Keith Thompson  <kst-u@mib.org> wrote:
>I'm not aware of any contradiction in this area.

I'd go more with 'unclear'.

An array is pretty well defined: the compiler can't add padding in
between rows, or at the start or end.  The size is what you expect
from a naive idea of 'array'.  The sequence is defined; the compiler
can't store the rows 'upside-down' or 'backwards'.

So we have elements b[0][0], b[0][1], ..., b[1][0], ..., b[3][4].  We
can take addresses &b[0][0], &b[0][1], ..., &b[1][0], ..., &b[3][4], and
of course those have type 'int *'.  And we have rows b[0], b[1], ...,
b[3].  We can take addresses &b[0], &b[1], ..., &b[3], and those have
type 'int (*)[5]'.

We can convert between the pointer types using a cast.  What guarantees
do we have here?  What things have to compare equal?  Maybe things
compare equal but don't work the same?  And we can do pointer
arithmetic.  Maybe we do a lot of pointer arithmetic and convert between
a bunch of types, before a dereference.

We need the standard specify these, not use vague hand-wavy phrases,
leaving everyone to form their own opinion about what it looks like when
you connect all the puzzle pieces.  We need to say what is defined or
undefined.

656 is weird because it talks about 'array subscript'.  Everyone knows
the brackets operator is shorthand for pointer addition and dereference.
So it should talk about pointer addition and dereference.  And then we
have cases where there is pointer addition independent of this, or many
steps before the dereference.

    int sethi(char *c)
    {
	c[0] = 'h';
	c[1] = 'i';
	c[2] = 0;
    }

We don't know that 'c[1]' is valid.  The type here is 'char *', a
pointer to a character.  So we just have to trust things here.

    int boom()
    {
	char c;

	/* here comes the 'boom' */
	sethi(&c);
	/* printf("%c\n", c); */
    }

But this is a caller/callee disagreement.  If it were pointer to array,
we could point our fingers better.

    int beam()
    {
	char c[20];

	c[0] = '*';
	sethi(&c[1]);
	strcat(&c[2], "\n");
	/* fputs(c, stdout); */
    }

Reverse of this, there's also the question of subtracting pointers where
arrays are involved.  656 doesn't appear to touch on this.  When is it
defined and does what is fairly obvious?  When is it undefined?

    int sub(int x)
    {
	int b[4][5];
	int *p;
	int *q;
	int d;

	p = &b[0][4];
	q = &b[1][0];
	d = (int) (q - p);
	/* printf("%d\n", d); */
	return d;
    }

>The point is that the standard gives compilers latitude to do something
>else.

Some rationale is not required, but it would be helpful.  It is that
they think most uses that violate that rule are probably erroneous?  It
is that you may produce better code?  Do they think it is a case where
the programmer can do what he intends, but he should express it clearly?
Is it a case where they're being conservative, because the implications
of the opposite are not well understood?

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