detecting invalid pointers

Joseph S. D. Yao jsdy at hadron.UUCP
Sat Mar 11 02:07:04 AEST 1989


In article <15495 at cup.portal.com> Kevin_P_McCarty at cup.portal.com writes:
>Is there any guaranteed way to detect an out of range pointer,
>i.e., one which is supposed to point into an array but might not?
>     int  x[TABLESIZE];
>     int  *p;

How about something on the order of:
	if (q != (int *) NULL &&
	    (i = q - x) >= 0 && i < TABLESIZE &&
	    q == &x[i]) {
		...
	}

I don't really think that the first comparison against NULL is nece-
ssary, but feel free to contradict.  (I know I couldn't stop ya.)

	Joe Yao		jsdy at hadron.COM (not yet domainised??)
	hadron!jsdy@{uunet.UU.NET,dtix.ARPA,decuac.DEC.COM}
	Xarc,arinc,att,avatar,blkcat,cos,decuac,\
	dtix,ecogong,empire,gong,grebyn,inco,    \
	insight,kcwc,lepton,lsw,netex,netxcom,    >!hadron!jsdy
	paul,phw5,research,rlgvax,seismo,sms,    /
	smsdpg,sundc,telenet,uunet              /



More information about the Comp.std.c mailing list