2

In Stroustrup's book, the chapter 5 drill has a small program where you insert selections that have purposeful errors in order to better understand error handling. One of the inserts is as follows:

vector<char> v(5); for (int i=0; i<=v.size(); ++i) ; cout << "Success!\n";

Placed into the larger code, it looks like this:

#include "std_lib_facilities.h"

int main()
  try {
    vector<char> v(5);
    for (int i=0; i<=v.size(); ++i) ;
    cout << "Success!\n";
    return 0;
  }
  catch (exception& e){
    cerr<<"error: "<<e.what()<<'\n';
    return 1;
  }
  catch (...){
    cerr<<"Oops: unknown exception!\n";
    return 2;
  }

I can fix the code just fine. To fix it, I remove the improper semicolon in the middle of the for statement, and I ostensibly change the <= to a < operator in the for statement so it doesn't result in a range error. This compiles and prints 5 "Success!" to the terminal.

The problem is that if I modify the program to retain the range error in order to see how that error would be handled, I still don't get an exception. If, for example, I simply remove the semicolon and leave the <= as is, it prints "Success!" 6 times. If I even make it do so for i<=v.size()+10, it still prints out "Success!" many times.

My understanding from Stroustrup is that this should not be how this is handled. It should throw an exception because <= makes it read 6 indexes for vector v and the 6th index should be out of range.

Can anyone help me understand why this program is not throwing an exception for the range error and not having any sort of problem?

I am using g++ as my compiler on a fresh install of Ubuntu 18.04 that I installed for the express purpose of learning to code. No special compiler arguments are used. Just "g++ -o NAME CODE.cpp".

Steve C
  • 31
  • 3
  • 3
    There is no range error. You are not using the vector in the loop. And certainly not v.at() – drescherjm Oct 17 '18 at 22:58
  • 2
    There's nothing in the code that would cause an exception. The author presumably left that out by mistake. – David G Oct 17 '18 at 22:59
  • 2
    Q: How can you get a range error with the vector ... if you're not actually *DOING* anything with the vector? Like writing to it, or reading from it? – paulsm4 Oct 17 '18 at 22:59
  • OK. I understand. So because nothing in the loop itself is trying actually *read* that index, it isn't a problem, even though it's running for that many times. Thank you. – Steve C Oct 17 '18 at 23:00
  • I suspect the code has been updated for this errata. – drescherjm Oct 17 '18 at 23:00
  • 1
    Accessing a vector, which isn't even used!, using [] outside its range just produces UB, an exception is not required. To get range checking you need to use `at()` instead. – doug Oct 17 '18 at 23:06
  • Yeah, I seems as if the intention was to highlight that error, but the code doesn't actually produce the error. It has taught me, in any case, that it isn't a problem to use such a condition in the for statement. Though it seems like it's probably a bad habit regardless. – Steve C Oct 17 '18 at 23:06
  • It’s ironic that Stroustrup was trying to write an error, but a typo made it run. There are lots of other cases in the text which are the exact opposite. The book has lots of typos/mistakes (at least my copy does), so you have to take it with a grain of salt. It’s a huge source of information, but you do have to see passed the mistakes. – Blair Fonville Oct 17 '18 at 23:43
  • I will bear that in mind, Blair. Thank you. – Steve C Oct 18 '18 at 00:01

0 Answers0