Range Checking with Arrays

For example, this can cause serious problems:
int scores[5];   // 5 elements, 0 - 4
scores[5] = 90;  // set the 6th element to 90!
Because the array was declared to be of size 5, writing a value to the 6th element can be catastrophic. Any data that exists at this location in memory will be erased and overwritten by the value 90. Errors like these are very hard to track down.

Previous page
Next page

Back to Lesson 11 Index
Back to Outline