Bounds checking

From Wikipedia, the free encyclopedia

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, C. Antony R. Hoare described his experience in the design of Algol 60, a language that included bounds checking. He said:

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 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.


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.

  • "The Emperor's Old Clothes", The 1980 ACM Turing Award Lecture, CACM volume 24 number 2, February 1981, pp 75-83. [1]
Advanced Search
Included Web Search Engines


Safe Search

close

Top Matching Results

Occasionally Search.com will highlight specialized results that are based on the context of your query. Examples of specialized results include specific links to news, images, or video.

Top Matching Results may highlight information from other Search.com pages, content from the CNET Network of sites, or third party content. The listings are based purely on relevance. Search.com does not receive payment for listings in this section but our partners that provide this data may get paid for listing these products.

Sponsored Links

This section contains paid listings which have been purchased by companies that want to have their sites appear for specific search terms and related content. These listings are administered, sorted and maintained by a third party and are not endorsed by Search.com.

Search Results

Search.com sends your search query to several search engines at one time and integrates the results into one list which has been sorted by relevance using Search.com's proprietary algorithm. You can customize the list of search engines included in your metasearch from the preferences.

The search engines that are used in your metasearch may allow companies to pay to have their Web sites included within the results. To view the Paid Inclusion policy for a specific search engine, please visit their Web site. Search.com does not accept payment or share revenue with any search engine partner for listings in this section.