Array bounds checking with C????

Henry Spencer henry at zoo.toronto.edu
Sun Aug 26 12:36:58 AEST 1990


In article <619.26d6cfb2 at iccgcc.decnet.ab.com> browns at iccgcc.decnet.ab.com (Stan Brown, Oak Road Systems) writes:
>int arr[4];
>
>sizeof arr/sizeof *arr is 4, so &arr[4] may not be legal.  Whether legal or
>not, the address is certainly outside the array.

It is, however, legal, by special dispensation of ANSI C.  You can't
dereference it, but it is required to behave properly and participate
properly in comparisons and arithmetic.  Note, this only applies to
the address "one after" the end of the array -- both "two after" the
end and "one before" the beginning of the array take you into the
twilight zone of undefined behavior.
-- 
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