MC logo
Array Errors
[^] CSc 220 Home
[Introduction] [Boolean and Control] [Functions] [Arrays and Pointers] [Dynamic Arrays] [Array Errors] [Command Array] [Standard Containers] [Structs and Classes] [Automatic Pointer] [Multi-File Programs] [Copying Objects] [Templates] [Inheritance] [Plain C I/O] [Type Odds And Ends] [Plain C Strings] [File I/O]
[Array Error I] [Array Error II] [Array Error III]
When Subscripting Goes Bad

As noted, C++ does not check that subscripts are within bounds. When an out-of-bounds reference is performed, the system completes it anyway. This will result in a fetch or a store at some location outside the array. Exactly where that lands depends on how the compiler has chosen to arrange memory, but it may be inside another variable, or inside some sort of system structure. If the operating system considers the address illegal, it will generally terminate the program. If not, the program will continue after changing or fetching some “random” variable. The resulting program behavior will be difficult to predict, and will vary from one system or compiler to another.

In this section are a few programs which perform illegal subscripts. Your results will almost certainly vary.