Newsgroups: comp.std.c Path: utzoo!henry From: henry@utzoo.uucp (Henry Spencer) Subject: Re: Out-of-bounds pointers Message-ID: <1989Oct3.162801.5708@utzoo.uucp> Organization: U of Toronto Zoology References: <1009@mtxinu.UUCP> Date: Tue, 3 Oct 89 16:28:01 GMT In article <1009@mtxinu.UUCP> ed@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@zoo.toronto.edu