Path: utzoo!mnetor!uunet!ncc!alberta!att-ih!ttrdc!levy From: levy@ttrdc.UUCP (Daniel R. Levy) Newsgroups: comp.lang.c Subject: Re: Is &a[NTHINGS] legal Message-ID: <2637@ttrdc.UUCP> Date: 1 May 88 05:03:07 GMT References: <12074@tut.cis.ohio-state.edu> <7806@brl-smoke.ARPA> Organization: AT&T, Skokie, IL Lines: 32 Summary: picky point In article <7806@brl-smoke.ARPA>, gwyn@brl-smoke.ARPA (Doug Gwyn ) writes: # In article <12074@tut.cis.ohio-state.edu> lvc@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,!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.