Out of range pointers

Mark Brader msb at sq.uucp
Sat Sep 24 04:10:42 AEST 1988


The conclusion given is right; the reason is wrong.

Richard Harter (g-rh at XAIT.Xerox.COM) writes:
> >As a side note, one argument for making x[-1] legal is that it permits
> >you to use sentinels in both directions.  I don't see that this is a
> >problem, regardless of architecture.  All that is required is that nothing
> >be allocated on a segment boundary...

Henry Spencer (henry at utzoo.uucp), no less, replies:
> The situation unfortunately isn't as symmetrical as it looks, because
> a pointer to an array element points to the *beginning* of the array
> element.

He must not have gotten over his cold yet.  The correct statement is:
a pointer to an array element *is typically implemented as* pointing to
the beginning of the array element.  Depending on the machine architecture,
it might be equally well implementable as a pointer to the *end* of the
array element.  Other implementations are also conceivable.

A pointer to anything points to *all* of the thing, at once.
The following code copies all of y over all of x, doesn't it?
(Assuming that x and y have types for which the operations are legal.)

	p = &x; q = &y; *p = *q;

I'm feeling rather sensitive about this point just now, because I've been
discussing by email with David Prosser, the editor of the Draft ANSI
Standard for C, the several errors in its descriptions of array and pointer
operations.  It appears that he and his predecessors made the same or
similar mistakes.

> Both practices have been technically illegal all along,
> so legitimizing both wasn't vitally necessary.  Since x[size] gets used
> a lot and is cheap to do, it was legalized.  Since x[-1] was rather more
> costly and is used less, it wasn't.

Rather, since x[size] gets used a lot and x[-1] is used less, *and an
implementation is possible on most or all machines where x[size] is cheap*,
it was appropriate to bless x[size].

Mark Brader		"True excitement lies in doing 'sdb /unix /dev/kmem'"
utzoo!sq!msb, msb at sq.com				-- Pontus Hedman



More information about the Comp.lang.c mailing list