Out-of-bounds pointers

Henry Spencer henry at utzoo.uucp
Wed Oct 4 02:28:01 AEST 1989


In article <1009 at mtxinu.UUCP> ed at mtxinu.COM (Ed Gould) writes:
>Is the following code conformant?  It's clear that it's not legal to
>dereference the pointer in its "illegal" state, but is the p++ line
>guaranteed to return it to a valid value? ...
>		p = buf;
>		p--;	/* p contains an illegal value: &buf[-1] */
>		p++;	/* hopefully, now p == &buf[0] */

The effect of the `p--' line is undefined, so all bets are off.  Pointer
arithmetic (not dereferencing) is guaranteed to be well-behaved when a
pointer just past the *end* of the array is involved, but no such promises
are made about pointers just before the *beginning*.  It may work; it may
dump core; it may yield random results.
-- 
Nature is blind; Man is merely |     Henry Spencer at U of Toronto Zoology
shortsighted (and improving).  | uunet!attcan!utzoo!henry henry at zoo.toronto.edu



More information about the Comp.std.c mailing list