There is a popular saying that states, “You don’t get something for nothing.” This holds true with array types. The “something” you get is faster executing code at the expense of the “nothing,” which is zero boundary checking. Remember, since C and C++ were designed to replace assembly language code, error checking was left out of the compiler to keep the code lean. Without any compiler error checking, you must be very careful when dealing with array boundaries. For example, the following program elicits no complaints from the compiler, yet it can change the contents of other variables or even crash the program by writing beyond the array’s boundary:
/* * norun.c * Do NOT run this C program * Copyright (c) Chris H. Pappas and William H. Murray, 1998 */
#include <stdio.h>
#define iMAX 10 #define iOUT_OF_RANGE 50
main( ) { int inot_enough_room[iMAX], index;
for(index=0; index < iOUT_OF_RANGE; index++) inot_enough_room[index]=index;