Array bounds checking with C????

Henry Spencer henry at zoo.toronto.edu
Sun Aug 26 08:37:54 AEST 1990


In article <26196 at mimsy.umd.edu> chris at 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 at zoo.toronto.edu   utzoo!henry



More information about the Comp.lang.c mailing list