In computer programming, bounds checking is the name given to any method of detecting whether or not an index given lies within the limits of an array. For example, accessing index 25 on an array of size 10 would be caught by bounds checking as an invalid index, because it does not lie within the specified size limit of the array.
A failed bounds check usually results in the generation of an exception.
Because performing bounds checking during every access of an array is inefficient and in some applications unacceptable, there are compiler technologies for eliminating bounds checking in many common cases when they are probably not needed; see bounds checking elimination.
Many programming languages, such as C, never perform automatic bounds checking, in the interest of efficiency. This has, however, been a source of innumerable errors, especially off-by-one errors and buffer overflows. Although a lack of bounds-checking is necessary in some scenarios, many programmers believe these languages sacrifice too much in their search for rapid execution. In his 1980 Turing Award lecture (1), C. Antony R. Hoare described his experience in the design of Algol 60, a language that included bounds checking. He said:
Mainstream languages that enforce run time checking include Ada, Visual Basic, Java and C#. The D programming language has run time bounds checking that is enabled or disabled with a compiler switch. C# also supports the notion of an unsafe region, a section of code which (among other things) temporarily suspends bounds checking in the interest of efficiency. This is useful for speeding up small time-critical bottlenecks without sacrificing the safety of the entire program.
This article is licensed under the GNU Free Documentation License.
It uses material from the
"Bounds checking".
Home Page • arts • business • computers • games • health • hospitals • home • kids & teens • news • physicians • recreation• reference • regional • science • shopping • society • sports • world