Xref: utzoo comp.std.c:884 comp.lang.c:16905 Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!purdue!haven!decuac!hadron!jsdy From: jsdy@hadron.UUCP (Joseph S. D. Yao) Newsgroups: comp.std.c,comp.lang.c Subject: Re: detecting invalid pointers Message-ID: <842@hadron.UUCP> Date: 10 Mar 89 16:07:04 GMT References: <15495@cup.portal.com> Reply-To: jsdy@hadron.UUCP (Joseph S. D. Yao) Distribution: usa Organization: Hadron, Inc., Fairfax, VA Lines: 23 In article <15495@cup.portal.com> Kevin_P_McCarty@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@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 /