Newsgroups: comp.lang.c Path: utzoo!henry From: henry@zoo.toronto.edu (Henry Spencer) Subject: Re: Array bounds checking with C???? Message-ID: <1990Aug25.223754.6004@zoo.toronto.edu> Organization: U of Toronto Zoology References: <7611@ucdavis.ucdavis.edu> <26196@mimsy.umd.edu> Date: Sat, 25 Aug 90 22:37:54 GMT In article <26196@mimsy.umd.edu> chris@mimsy.umd.edu (Chris Torek) writes: >>Why is it that most C compilers don't seem to [check array bounds]? > >Mostly because it is hard. Given `int *p', is `p[-1] = 3' valid? >That depends on the value of p.... Given the all-pervasive nature of pointers in C, about the only way you can do bounds checking is to have each pointer haul along the range of legal subscripts for itself. This gets tricky in spots but is feasible. Unfortunately, it imposes a lot of overhead on every pointer manipulation, so it's badly unsuited to anything but a specialized debugging compiler, and we don't have many of those. Personally, I've long had a theory that doing this sort of checking at compile time rather than run time ought to be feasible -- any competent programmer takes care to avoid overrunning arrays, and given the limits of the human mind, it ought to be possible for the compiler to duplicate this reasoning, possibly with some help -- but it will not be easy. -- Committees do harm merely by existing. | Henry Spencer at U of Toronto Zoology -Freeman Dyson | henry@zoo.toronto.edu utzoo!henry