PDA

View Full Version : Runtime QVector "index out of range" error. How to fix it?



robgeek
13th May 2015, 05:43
Good evening!

What is wrong with the following code? I'm trying to use "insertPattern" method to return a QVector<Number>("Number" is an object i created) to "patternList". Then, push_back it into a QList. I'm not getting any compilation error message, but when i run it i get that error!


QList<QVector<Number>> RegNumber::patternList(QList<QVector<Number>> pl, int array[], int sizeArray)
// Irrelevant code here.
for(...) {//If i delete this "for" and call "insertPattern" one time, the program runs fine.
// Irrelevant code here.
pl.push_back( insertPattern(array, sizeArray) );
}


QVector<Number> RegNumber::insertPattern(int array[], int sizeArray)
{
QVector<Number> pattern;
pattern.push_back( Number(0, 1) );

for(int i = 0; i < sizeArray; i++)
{
if(array[i] != 0)
pattern.push_back( Number((i + 2), array[i]) ) ;
}

return pattern;
}


ASSERT failure in QVector(T)::operator[]: "index out of range", file C:\Qt\5.4\mingw491_32\include\QtCore/qvector.h, line 396

How can i fix it?

wysota
13th May 2015, 07:05
Is this your actual code? What you posted wouldn't cause such error. You are not using the indexing operator on a vector here anywhere. Post your real code, please.

robgeek
13th May 2015, 15:02
Actually i'm using index operator, but in QList, not in QVector, that's why i didn't post my entire code. If i try to insert only one QVector in that QList the program runs fine, but if i try to enter with other QVectors i get that error message.

The following code is the entire "patternList" method, the other one, "insertPattern" i posted in my first post.

QList<QVector<Number>> RegNumber::patternList(QList<QVector<Number>> pl, int array[], int sizeArray) {
int sizeLst = pl.size( );
int sizeVec, j;
bool equal = true;

if( sizeLst ) {
for(int i = 0; i < sizeLst; i++) {

sizeVec = pl[i].size( );
if((sizePatternFound(array, sizeArray)) == (sizeVec - 1)) {

equal = true;
for(j = 1; j < sizeArray; j++) {

if((j + 1) != pl[i][j].val)
equal = false;
else if(array[j - 1] != pl[i][j].acum)
equal = false;
}

if( equal ) {
pl[i][0].acum += 1;
i = sizeLst;
}
}
else {
equal = false;
}
}

if( !equal ) {
pl.push_back( insertPattern(array, sizeArray) );
}
}
else {
pl.push_back( insertPattern(array, sizeArray) );
}

return pl;
}

wysota
13th May 2015, 15:08
You are using vector's indexing operator three times -- in lines #15, #17 and #22.The last one can assert if the vector is empty, the previous two when sizeArray has wrong value (off by one?).

robgeek
13th May 2015, 15:42
So, try to imagine a list of vectors(arrays with different size of Number) in which the first node "pl[i][0].acum" of each is used to calculate(increment) how many times that combination of Number(pl[i][1...n].val and acum) appears in other place of my program that doesn't show any arror. Tha't why i used "pl[i][0].acum += 1" in line number 22, because that pattern of Number is in the list so i don't need to insert another one, just increment.

I regret of one thing actually, instead of using array and be forced to do that weird thing in lines 15 and 17 i should use QVector of Number. The code would be much much more legible. Mistakes you see only after doing!

Doing this way i just have to check if two QVectors(the in the position x of the list and the one passed as arg) are equal, if yes, increments, if not insert him in the list.

wysota
13th May 2015, 16:18
I'm only pointing out places which might generate the error. You can place asserts before using the operators that check validity of the call.