
-----------------------------------
SJ
Tue Dec 30, 2008 1:03 pm

Array out of bounds?
-----------------------------------
I've came across this today

    vector  v;
    v.push_back (5);

    // version1, this doens't give an error
    for (int i = 0; i+2 < v.size(); i++)
      cout = v.size(). But since i = 0, and v.size() = 1, it doesn't output anything (because i + 2 > v.size() ).

Although in Version 2, you want i >= v.size() - 2 for the loop to exit.....however, since i = 0 initially and v.size() - 2 = -1, the loop goes on forever (because i increases by 1, and therefore can never by less than or equal to -1)

That's why it crashes....

-----------------------------------
SJ
Tue Dec 30, 2008 1:32 pm

RE:Array out of bounds?
-----------------------------------
ahh right, thank you :)

-----------------------------------
A.J
Tue Dec 30, 2008 1:52 pm

Re: Array out of bounds?
-----------------------------------
no probs :)

-----------------------------------
md
Tue Dec 30, 2008 3:33 pm

RE:Array out of bounds?
-----------------------------------
Errm... 0 >= -1 so the second loop should exit right away... this is one of those times a debugger might help - or not waking up at 3:00 pm...
