All Topics  
Bounds checking

 

   Email Print
   Bookmark   Link






 

Bounds checking



 
 
In computer programming
Computer programming

Computer programming is the process of writing, testing, debugging/troubleshooting, and maintaining the source code of computer programs. This source code is written in a programming language....
, bounds checking is any method of detecting whether a variable is within some bounds before its use. It is particularly relevant to a variable used as an index
Index (information technology)

In computer science, an index can be:# an integer which identifies an array element# a pointer data element.# a data structure that enables sublinear-time lookup...
 into an array to ensure its value lies within the bounds of the array
Array

In computer science, an array is a data structure consisting of a group of element s that are accessed by index . In most programming languages each element has the same data type and the array occupies a contiguous area of computer memory....
. For example: a value of 32768 about to be assigned to a sixteen-bit signed integer variable (whose upper bounds are -32768 to +32767), or accessing element 25 on an array with index range 0 through 9 only.






Discussion
Ask a question about 'Bounds checking'
Start a new discussion about 'Bounds checking'
Answer questions from other users
Full Discussion Forum



Encyclopedia


In computer programming
Computer programming

Computer programming is the process of writing, testing, debugging/troubleshooting, and maintaining the source code of computer programs. This source code is written in a programming language....
, bounds checking is any method of detecting whether a variable is within some bounds before its use. It is particularly relevant to a variable used as an index
Index (information technology)

In computer science, an index can be:# an integer which identifies an array element# a pointer data element.# a data structure that enables sublinear-time lookup...
 into an array to ensure its value lies within the bounds of the array
Array

In computer science, an array is a data structure consisting of a group of element s that are accessed by index . In most programming languages each element has the same data type and the array occupies a contiguous area of computer memory....
. For example: a value of 32768 about to be assigned to a sixteen-bit signed integer variable (whose upper bounds are -32768 to +32767), or accessing element 25 on an array with index range 0 through 9 only. The first is also known as range check
Range check

In computer programming, a range check is a check to make sure a number is within a certain range. This is often used with arrays, as using a number outside of the upper range in an array may cause the program to crash, or may introduce security vulnerabilities ....
ing, the second as index checking
Index checking

In computer programming, much use is made of simple variables given names such as X, I, Enough, etc. A compiler, in generating the machine code will have some scheme for assigning computer storage locations to hold the values of such variables and then, whenever there is mention of a name, the compiler will generate code that refers to...
.

A failed bounds check usually results in the generation of some sort of exception
Exception handling

Exception handling is a programming language construct or computer hardware mechanism designed to handle the occurrence of exceptions - special conditions that change the normal flow of execution....
 signal.

Because performing bounds checking during every usage is time-consuming it is not always done. Bounds checking elimination are compiler technologies that eliminate unneeded bounds checking in many common cases.

Many programming languages, such as C
C (programming language)

C is a general-purpose computer programming language originally developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories to implement the Unix operating system....
, never perform automatic bounds checking, in the interest of speed. However, this leaves uncaught many off-by-one error
Off-by-one error

An off-by-one error is a logical error involving the discrete equivalent of a boundary condition. It often occurs in computer programming when an control flow#Loops iterates one time too many or too few....
s and buffer overflow
Buffer overflow

In computer security and computer programming, a buffer overflow, or buffer overrun, is an Anomaly in software condition where a process attempts to store data beyond the boundaries of a fixed-length buffer ....
s. Many programmers believe these languages sacrifice too much for rapid execution. In his 1980 Turing Award
Turing Award

The A. M. Turing Award is given annually by the Association for Computing Machinery to "an individual selected for contributions of a technical nature made to the computing community....
 lecture, C. Antony R. Hoare described his experience in the design of Algol 60
Algol

Algol , known colloquially as the Demon Star, is a bright star in the constellation Perseus . It is one of the best known eclipsing binary, the first such star to be discovered, and also one of the first variable stars to be discovered....
, a language that included bounds checking, saying:

A consequence of this principle is that every occurrence of every subscript of every subscripted variable was on every occasion checked at run time against both the upper and the lower declared bounds of the array. Many years later we asked our customers whether they wished us to provide an option to switch off these checks in the interest of efficiency on production runs. Unanimously, they urged us not to - they already knew how frequently subscript errors occur on production runs where failure to detect them could be disastrous. I note with fear and horror that even in 1980, language designers and users have not learned this lesson. In any respectable branch of engineering, failure to observe such elementary precautions would have long been against the law.


Mainstream languages that enforce run time checking include Ada, Visual Basic
Visual Basic

'Visual Basic' is the third-generation programming language event-driven programming and integrated integrated development environment from Microsoft for its Component Object Model programming model....
, Java
Java (programming language)

Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java ....
, JavaScript
JavaScript

JavaScript is a scripting language widely used for client-side web development. It was the originating Programming language dialect of the ECMAScript standard....
, PHP
PHP

PHP is a scripting language originally designed for producing dynamic web pages. It has evolved to include a command line interface capability and can be used in Standalone software Graphical user interface....
, Python
Python (programming language)

Python is a general-purpose high-level programming language. Its design philosophy emphasizes code readability. Python's core syntax and semantics are Minimalism , while the standard library is large and comprehensive....
, Ruby
Ruby (programming language)

Ruby is a dynamic programming language, reflection , general purpose object-oriented programming language that combines syntax inspired by Perl with Smalltalk-like features....
, Haskell
Haskell (programming language)

Haskell is a standardized, purely functional programming language with non-strict programming language, named after logician Haskell Curry. The goals of the language are described as:...
, Lisp, and C#. The D
D (programming language)

The D programming language, also known simply as D, is an Object-oriented programming, Imperative programming, Multi-paradigm programming language system programming language by Walter Bright of Digital Mars....
 and OCaml languages have run time bounds checking that is enabled or disabled with a compiler switch. C# also supports unsafe regions: sections of code that (among other things) temporarily suspend bounds checking in the interest of efficiency. These are useful for speeding up small time-critical bottlenecks without sacrificing the safety of the entire program.

Data Quality


In the context of data collection and data quality, bounds checking refers to checking that the data is not trivially invalid. For example, a percentage measurement must be in the range 0 to 100; the height of an adult person must be in the range 0 to 3 meters.