Sunday, July 12, 2009

Why doesn't C++ provide bound checking on array operations and if the programmer want to do it,?

what steps are needed to prevent the problem of over running of an array in C++?

Why doesn't C++ provide bound checking on array operations and if the programmer want to do it,?
Because that costs execution time on every access. For code that is at the core of a CPU intensive process, this is a no-no. Requiring it would force everyone to use even if they did not want it.





The STL containers optionally provide checking. For the template vector, using array subscripting is not checked, but the member function at() is checked.





std::vector%26lt;int%26gt; vi(3);


vi[0] = 2; // not bounds checked


vi.at(4) = 3; // bounds checked - will throw an exception
Reply:In java, there is array bound-checking but in C++, there is none.


If you want to avoid index out of bound error, you need to subtract 1 to the counter you are using since array starts at index 0.


No comments:

Post a Comment