Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!wuarchive!zaphod.mps.ohio-state.edu!rpi!leah!bingvaxu!vu0310 From: vu0310@bingvaxu.cc.binghamton.edu (R. Kym Horsell) Newsgroups: comp.lang.c Subject: Re: Array bounds checking with C???? Message-ID: <3890@bingvaxu.cc.binghamton.edu> Date: 25 Aug 90 17:59:56 GMT References: <7611@ucdavis.ucdavis.edu> Reply-To: vu0310@bingvaxu.cc.binghamton.edu.cc.binghamton.edu (R. Kym Horsell) Organization: SUNY Binghamton, NY Lines: 24 In article <7611@ucdavis.ucdavis.edu> kuan@iris.ucdavis.edu (Frank [Who me?] Kuan) writes: > > Why is it that most C compilers don't seem to support this > nifty little feature? I guess this isn't usually included because (a) array indexing is subsumed by pointer arithmetic & this is *much* harder (i.e. impossible in general) to check; (b) arrays can be declared with no bounds, i.e. extern long arr[]; which implies either a smart linker and/or runtime support for array descriptions -- the antithesis of C (c) is is easy enough to do it yourself with macros: extern Thingy arr_[]; #define arr(i) arr_[chkbnds(i,0,max_ind_of_arr_)] int chkbnds(ind,lwb,upb) { if(ind>=lwb && ind<=upb) return ind; /* chunder */ exit(-1); } (note that we need a routine here so ``ind'', which may include side-effects, doesn't get evaluated twice). -Kym Horsell