Out-of-bounds pointers

Ed Gould ed at mtxinu.COM
Tue Oct 3 11:01:38 AEST 1989


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?


	void
	f() {
		char bug[100];
		char *p;

		p = buf;

		p--;	/* p contains an illegal value: &buf[-1] */
		p++;	/* hopefully, now p == &buf[0] */

		if(p == buf)
			printf("It worked\n");
		else
			printf("It failed\n");
	}



More information about the Comp.std.c mailing list