Array bounds checking with C????

R. Kym Horsell vu0310 at bingvaxu.cc.binghamton.edu
Sun Aug 26 03:59:56 AEST 1990


In article <7611 at ucdavis.ucdavis.edu> kuan at 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



More information about the Comp.lang.c mailing list