Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I getting a "vector iterator + offset out of range" error?

The following code is causing this error:

if (bestLine.size() > searchDepth - depth)
    bestLine.erase(bestLine.begin(), bestLine.end() - searchDepth - depth);

When I checked the value of searchDepth - depth at the time of the error, it was 0.

So essentially,

if (bestLine.size() > 0)
    bestLine.erase(bestLine.begin(), bestLine.end());

is causing this error. (Or not. See comments below.)

To my knowledge the above code should erase the entire vector, which is the desired behavior in this case.

What am I doing wrong?

like image 497
Svad Histhana Avatar asked Dec 14 '25 16:12

Svad Histhana


2 Answers

Try adding parentheses to your expression: bestLine.end() - (searchDepth - depth). The result is very different if simply evaluated left-to-right.

like image 80
Blastfurnace Avatar answered Dec 16 '25 06:12

Blastfurnace


You check if bestLine.size() is greater then searchDepth - depth, but then you substract searchDepth + depth. Change the sign before depth in the subtraction: bestLine.erase(bestLine.begin(), bestLine.end() - searchDepth *+* depth);

like image 28
Ivaylo Strandjev Avatar answered Dec 16 '25 04:12

Ivaylo Strandjev