Overrunning Array Boundaries

Since C doesn't check array subscripts for validity, you must keep track of array boundaries on your own. For instance, if you initialize a five-character array,

char sample[] = "ABCD";

and refer to a nonexistent array element,

sample[9] = 'X';

QuickC doesn't signal an error, although the second statement overwrites memory outside the array. It stores a character in element 9 of an array that contains only 5 elements.

The same problem can occur when accessing an array through a pointer:

char sample[] = "ABCD";

char *ptr = sample;

*--ptr = 'X'; /* Error! */

The code overwrites the byte in memory below the array. To avoid such problems, confine all array operations within the range used to declare the array.