Is &a[NTHINGS] legal

Daniel R. Levy levy at ttrdc.UUCP
Sun May 1 15:03:07 AEST 1988


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?
# 
# Yes, it is.  This kind of code is quite pervasive, and if you
# consider that NTHINGS might have been defined as 0 it is impossible
# to avoid (in fact in that situation your case 2 is invalid).
# 
# Every object must have at least one addressable cell beyond it,
# but not necessarily in front of it.  The reason the latter is not
# required is that &a[-1] may be MANY bytes in front of allocated
# storage if the array element is large, but &a[NTHINGS] will be
# just one byte past the valid array locations.

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.
-- 
|------------Dan Levy------------|  Path: ihnp4,<most AT&T machines>!ttrdc!levy
|              AT&T              |  Weinberg's Principle:  An expert is a
|       Data Systems Group       |  person who avoids the small errors while
|--------Skokie, Illinois--------|  sweeping on to the grand fallacy.



More information about the Comp.lang.c mailing list