Bounds checking

Bounds checking

In computer programming, 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 into an array to ensure its value lies within the bounds of the array. 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 checking, the second as index checking.

A failed bounds check usually results in the generation of some sort of exception 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, never perform automatic bounds checking, in the interest of speed. However, this leaves uncaught many off-by-one errors and buffer overflows. Many programmers believe these languages sacrifice too much for rapid execution. In his 1980 Turing Award lecture, C. Antony R. Hoare described his experience in the design of Algol 60, 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, Java, JavaScript, Python, Ruby, Haskell, Lisp, and C#. The D 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.

References

* "The Emperor's Old Clothes", The 1980 ACM Turing Award Lecture, CACM volume 24 number 2, February 1981, pp 75-83. [http://portal.acm.org/citation.cfm?id=358561]


Wikimedia Foundation. 2010.

Игры ⚽ Нужен реферат?

Look at other dictionaries:

  • Bounds-checking elimination — In computer science, bounds checking elimination is a compiler optimization useful in programming languages or runtimes that enforce bounds checking, the practice of consistently checking every index into an array to verify that the index is… …   Wikipedia

  • 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 …   Wikipedia

  • Beating the bounds — is an ancient custom still observed in many English parishes. The community would walk the boundaries of the parish, to share the knowledge of where they lay, and to pray for protection and blessings for the lands.CeremonyIn former times when… …   Wikipedia

  • Comparison of Java and C++ — Programming language comparisons General comparison Basic syntax Basic instructions Arrays Associative arrays String operations …   Wikipedia

  • Buffer overflow — In computer security and programming, a buffer overflow, or buffer overrun, is an anomalous condition where a process attempts to store data beyond the boundaries of a fixed length buffer. The result is that the extra data overwrites adjacent… …   Wikipedia

  • Array data type — Not to be confused with Array data structure. In computer science, an array type is a data type that is meant to describe a collection of elements (values or variables), each selected by one or more indices that can be computed at run time by the …   Wikipedia

  • Compiler optimization — is the process of tuning the output of a compiler to minimize or maximize some attributes of an executable computer program. The most common requirement is to minimize the time taken to execute a program; a less common one is to minimize the… …   Wikipedia

  • C (programming language) — C The C Programming Language[1] (aka K R ) is the seminal book on C …   Wikipedia

  • Comparison of programming languages (array) — Programming language comparisons General comparison Basic syntax Basic instructions Arrays Associative arrays String operations …   Wikipedia

  • C standard library — The C Standard Library consists of a set of sections of the ANSI C standard in the programming language C. They describe a collection of headers and library routines used to implement common operations such as input/output[1] and string handling …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”