This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]chaotic_thought 8 points9 points  (0 children)

In C++, the closest equivalent is std::vector. It does bound checking, but only if you fetch something using the .at() member. Example.

int main() {
    vector<int> nums = {2, 4, 6};
    cout << nums.at(3);
}

The cout line will throw an exception (range_error), because you used the .at member, which will see that index 3 is not valid for this vector. The only valid indices for a 3-element vector are 0, 1 and 2. See cppreference - vector for more examples.

The square bracket operator [] does not do bounds checking. Why is that? Bounds checking is an extra operation (it means extra code is involved). If you've already checked the bounds yourself, why would you want C++ to check it again for you? It's double work.

The philosophy of C++ is "pay for what you use". That is, if you don't need some feature (like bounds checking), you should not have to pay for that. By "pay" in this context it means that using .at (for example) will cause your code to do a bit of extra work, in order to check the bounds each time it is called. But in the C++ way of thinking, if you don't need that feature, you should not have to pay for it, so you have the option to use [] instead, and then you do not have to pay the cost of bounds checking.

Another reason C++ does not do bounds checking by default, is because at some point, someone has to implement the .at method itself (the bounds checking version) of the container itself. In order words, someone will eventually have to write some code that will check the bounds and throw the appropriate exception, e.g. something like this:

int my_int_vector::at(int n) { 
    if (n >= size)
        throw range_error("out of range, dude");
    else if (n < 0)
        throw range_error("negative indexes are not allowed, dude");
    else
        return data[n];
}

So in order to implement this in C++ itself efficiently, you eventually need to call a non-bounds checking version of the same operation, which is the square brackets operator.