- Some languages do automatic range checking; this is also called bounds checking
- Each time the program accesses an element of an array, the index is checked to see if it is between 0 and the size of the array - 1
- C++ does NOT do any range checking whatsoever; it is entirely up to the programmer to avoid any runtime errors that may occur due to invalid array indexing
- If the programmer fails to avoid "running off the end" of an array, the program may become unstable; if it doesn't crash right away, it may crash sometime later
- It can be very time consuming to verify each and every array access, so C++ avoids it. C/C++ was designed to be very efficient at the expense of unsafe operations
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.