PDA

View Full Version : Array error: Don't know how to fix it.



robgeek
3rd February 2016, 21:33
Hi!

I would like to know why the following code shows me an error message when i run my program. I have no compile error message, just when i start the program. I commented some line to you understand better the code.


QList<QVector<Number> > Game::dozensSeqDrawn(int tm) {
int acum, ini, aux[size]; // size is 1786
ini = setStartRead( tm ); // sets a start point to read the matrix
QList<QVector<Number>> pl; //Number has (int val, int acum) as attributes

for(int dz = 0; dz < 6; dz++) {
arrayResetZero(aux, size); // sets to 0 each cell in the array
acum = 0;

for(int i = ini; i < size; i++) {

if( dozenInArray(historic[i], dz) ) {//historic[i] is a qvector
acum++;
}
else if(acum > 0) {
aux[(acum - 1)] += 1;
acum = 0;
}

}

pl.push_back( seqDrawnVector(aux, size) );
}

return pl;
}

QVector<Number> Game::seqDrawnVector(int array[], int length) {
QVector<Number> list;

for(int i = 0; i < length; i++)
if(array[i] != 0)
list.push_back( Number((i + 1), array[i]) );

return list;
}


ASSERT failure in QList<T>:: operator[]: "index out of range", file /usr/include/qt/QtCore/qlist.h, line 518
Aborted (core dumped)

I tried to change a little bit my code to this:

QList<QVector<Number> > Game::dozensSeqDrawn(int tm) {
QList<QVector<Number>> pl; //Number has (int val, int acum) as attributes
QVector<Number> line;

line.push_back(Number(-1, -1));
line.push_back(Number(-1, -1));
line.push_back(Number(-1, -1));

pl.push_back( line );

return pl;
}

Same error message.

d_stranz
3rd February 2016, 23:56
I don't see anything in the code you post here that could cause that error. You only push things onto an empty list, you never read it, at least not in the posted code. Your error comes from someplace else in your code, most likely.

Have you correctly initialized whatever variable you are using to index into the list? Do you set it to less than zero or greater than the list range somewhere? Is your list ever empty when you try to read from it?

Do you know how to use the debugger? Run it with a debug version of your program and see where it fails.

robgeek
4th February 2016, 02:54
Thanks for your answer! I'll try qdebugger. I'll post results here later.