Out-of-bounds pointers
Larry Jones
scjones at sdrc.UUCP
Wed Oct 4 08:01:24 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? What would it (be expected
> to) print?
>
> p = buf;
> p--; /* p contains an illegal value: &buf[-1] */
> p++; /* hopefully, now p == &buf[0] */
Nope. Once you've decremented p you've entered the twilight zone
of undefined behaviour where your program could dump core, play
the Star Spangled Banner, or continue on. If it continued, the
increment may or may not set it pointing to buf again.
----
Larry Jones UUCP: uunet!sdrc!scjones
SDRC scjones at SDRC.UU.NET
2000 Eastman Dr. BIX: ltl
Milford, OH 45150-2789 AT&T: (513) 576-2070
"I have plenty of good sense. I just choose to ignore it."
-Calvin
More information about the Comp.std.c
mailing list