Array bounds checking with C????

Chip Salzenberg chip at tct.uucp
Thu Sep 6 02:16:15 AEST 1990


Larry Campbell flames compiler writers for generating different code
for these fragments:

>    /* Fragment 1 */
>    for (p = array; p < &array[ARRAY_SIZE]; p++)
>	*p++ = '\0';
>
>    /* Fragment 2 */
>    for (i = 0; i < ARRAY_SIZE; i++)
>	array[i] = '\0';

Mr. Campbell here makes a good point about compiler optimization.
However, Mr. Campbell also writes:

>If the answer to my question is "no", then the only excuse for permitting
>array indices past the end of an array is for compiling old, crufty code
>from clay tablets.

Many C programmers use pointers for clarity, not just out of habit.
My code, including my brand new code, frequently uses pointer
navigation within arrays.  This feature is useful, and its use is not
confined to "old, crufty code".

Other programming languages get along without pointers into arrays.
That's one of the reasons I don't use those other languages.
-- 
Chip Salzenberg at Teltronics/TCT     <chip at tct.uucp>, <uunet!pdn!tct!chip>



More information about the Comp.lang.c mailing list