943,772 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4643
  • C++ RSS
Feb 5th, 2008
0

Bound Checking

Expand Post »
Can over running of array lead to the catastrophic failures? If ‘Yes’ then why does not C++ provide bound checking on array operations and who is responsible to prevent array overruns?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
faisaly is offline Offline
21 posts
since May 2007
Feb 5th, 2008
0

Re: Bound Checking

> Can over running of array lead to the catastrophic failures?
Yes.
>who is responsible to prevent array overruns?
The programmer is. It's one of the things the programmer should anticipate and prevent.
There are some functions that can help you with this, like strlen for an array of chars.

Niek
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Feb 5th, 2008
0

Re: Bound Checking

for storing and accessing a sequence of objects in memory, you cannot have a construct more efficient (in terms of both time and space) than an array. it is, however, also a very low level data structure with two fundamental properties:
a. an array doesn't know its own size.
b. the name of an array decays to a pointer to its first element very easily.
these have many consequences:
1. any checking has to be provided by the programmer. a good programmer is acutely aware of this and usually gets the size right, but it's extra work and someone can make a mistake.
2. there can be no array assignment
3. arrays with automatic/static storage durations are of a fixed size determined at compile time. (and arrays with a dynamic storage duration decay into pointers before you can access them. C99 allows variable array bounds for local arrays, but VLAs do not come without their own problems.)
4. array decay into pointers interact very badly (incorrectly) with inheritance. for example
c++ Syntax (Toggle Plain Text)
  1. struct base { void foo() ; /* ... */ };
  2. struct derived : base { /* ... */ };
  3. void bar( base array[], size_t sz )
  4. { for( size_t i=0 ; i<sz ; ++i ) array[i].foo() ; }
  5. int main()
  6. {
  7. derived d[20] ;
  8. bar( d, 20 ) ; // disaster
  9. }

in almost all cases there are better (easier to write, easier to read, less error prone, and almost as fast) alternatives. for example std::vector<> provides a fast unchecked array subscript operator, and the member function at with checked element access. many implementations (most notably microsoft) also provide bounds-checked iterators (and if they do not, it is easy enough to write one of your own). in general, try to avoid using arrays in c++.

several approaches to the problem of bounds checking have been attempted. these include
a. static checking
i. modify c in a fashion that makes such errors easier to detect.
ii. look only at the source code

b. fat pointer systems
replace every pointer value with a structure that describes the valid range for the pointer, and replace every pointer read/write with code that checks this structure.

c. pointer guards
variation on the theme of fat pointers. Rather than keep the extended data in the pointer itself, a new object is generated which can be located given the location of the pointer it is associated with. This object stores the meta-data associated with the pointer.

d. fence posts
an approach to bounds checking based on placing regions at the end of every allocated block, and trapping any access to those regions. commonly implemented in hardware.

e. object tracking
accomplished by noting when every object is constructed and building a data structure that can identify the object located in a given memory address. when pointer access/arithmetic occurs, the original object is looked up.

these are some popular bounds-checking implementations
ccured http://manju.cs.berkeley.edu/ccured/
cyclone http://www.research.att.com/viewProject.cfm?prjID=67
splint http://www.splint.org/
tinycc http://fabrice.bellard.free.fr/tcc/
valgrind http://valgrind.org/info/
rational purify http://www-306.ibm.com/software/awdtools/purify/
electric fence http://directory.fsf.org/project/ElectricFence/
parasoft insure++ http://www.parasoft.com/jsp/products...product=Insure

more information: http://www.doc.ic.ac.uk/teaching/pro...ewSuffield.pdf
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Linked list using ctrl+z out from loop.But doesn't work well...
Next Thread in C++ Forum Timeline: Hung when compiling





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC