Is &a[NTHINGS] legal

Carl Paukstis carlp at iscuva.ISCS.COM
Wed May 4 09:17:46 AEST 1988


In article <2637 at ttrdc.UUCP> levy at ttrdc.UUCP (Daniel R. Levy) writes:
=In article <7806 at brl-smoke.ARPA>, gwyn at brl-smoke.ARPA (Doug Gwyn ) writes:
=# In article <12074 at tut.cis.ohio-state.edu> lvc at tut.cis.ohio-state.edu (Lawrence V. Cipriani) writes:
=# >Is it legal to apply the & (address of) operator to an array
=# >element that is non-existent?  Given:
=# >	sometype a[NTHINGS], *p;
=# >Should:
=# >	for (p = a; p < &a[NTHINGS]; p++)	/* 1 */
=# >be written as:
=# >	for (p = a; p <= &a[NTHINGS-1]; p++)	/* 2 */
=# >		...
=# >Will 1 be guaranteed to work in ANSI-C?
=# 
=# ...
=# Every object must have at least one addressable cell beyond it,
=# but not necessarily in front of it.  

I thought that the requirement was, in effect, that there must be at least
one valid ADDRESS beyond the composite object.  Maybe this is what you're
saying, but...

=A picky point if you will:  would not one expect that &a[NTHINGS] must have at
=and beyond it at least as many bytes as the smallest object having the same
=alignment requirement as an object having the type of a[0]?  Imagine a[]
=being laid out in a segment of memory as close to the "top" as possible while
=still having &a[NTHINGS] be valid, and you'll see what I mean.

The above notion follows from a quick reading of Mr. Gwyn's original
followup.  It seems, however, that there need not be an addressable OBJECT
beyond the array bound, and it is an error to attempt to dereference
a[NTHINGS+1].  There must be a valid ADDRESS (or at least simulation of
one), so that size calculations will work, mostly, but also so that
bound-checking as proposed in the original posting is possible.

Maybe I've just added to the confusion, here...

-- 
Carl Paukstis    +1 509 927 5600 x5321  |"I met a girl who sang the blues
                                        | and asked her for some happy news
UUCP:     carlp at iscuvc.ISCS.COM         | but she just smiled and turned away"
          ...uunet!iscuva!iscuvc!carlp  |                    - Don MacLean



More information about the Comp.lang.c mailing list