Question:
Why doesn't C++ provide bound checking on array operations and if the programmer want to do it,?
anonymous
2008-01-30 03:47:14 UTC
what steps are needed to prevent the problem of over running of an array in C++?
Three answers:
DogmaBites
2008-01-30 07:02:46 UTC
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 vi(3);

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

vi.at(4) = 3; // bounds checked - will throw an exception
lei
2008-01-30 04:00:19 UTC
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.
anonymous
2016-12-28 12:37:43 UTC
in case you declare an array like int a[d3d944682a44259755d38e6d163e820d3d944682a44259755d38e6d163e820]; Then valid element indexes are 10 to 9. Any attempt different than this variety ought to offer strengthen an blunders. in many situations, Java, C# etc are equipped with remarkable run time help as a results of fact of which this kind of blunders would be thrown by making use of the run time equipment. the place as C, provides blunders which contain memory phase violation or unlawful get right of entry to of memory etc style blunders.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Search results for 'Why doesn't C++ provide bound checking on array operations and if the programmer want to do it,?' (Questions and Answers)
5
replies
can i get question answer of asp.net ?
started 2006-10-11 00:02:47 UTC
software
6
replies
help needed in c program?
started 2007-04-20 06:45:26 UTC
programming & design
Loading...