PDA

View Full Version : QVector issue going out of bounds



bnosam
19th July 2014, 05:57
Whenever I run my code I get an error stating the vector went out of bounds. The vector is declared as



QVector <CCell> grid;

in a class, where CCell is just a class which contains a couple functions for setting its values.

So in this class that the vector is declared, I initialize it with

resize(width * height);

So after breaking pointing the vector gets sized correctly to the value 600 (30 * 20).
So after investigating where this would error out it is this code here immediately from the constructor:


void CGrid::clearMap()
{
int i = 0;
int j = 0;

for(i = 0; i < width; i++)
{
for(j = 0; j < height; j++)
{
grid[i * width + j].setCellVal(0);
}
}
}


for whatever reason when following this in breakpoints, j immediately is initialized to 0, then after going through the first for loop, it jumps immediately to 20 (or whatever the height value is), immediately throwing it out of bounds for the vector.
I have no code that changes this value to 20 or interacts with it, so there is no way that I can think of for it to do this.

Any ideas? I'm using the QtCreator 3 IDE with MSVC2010 compiler.

jefftee
19th July 2014, 06:13
Can you share your code for CCell::setCellValue()? Also, what are the values of width and height when you are debugging?

Edit: Ah, nevermind, now I see the problem... Try this instead:



void CGrid::clearMap()
{
int i = 0;
int j = 0;

for(i = 0; i < width; i++)
{
for(j = 0; j < height; j++)
{
grid[i * height + j].setCellVal(0);
}
}
}

You were blowing out the bounds of the QVector by computing an index outside of the range of your QVector that contains 600 items. As soon as you hit index 600, kaboom! Bugs like this are easier to spot if you use a variable for the index instead of a computation. For example:


int index = i * height + j;
grid[index].setCellVal(0);

The above would have been much easier to spot IMHO...

bnosam
19th July 2014, 06:32
Setcellvalue is just basically


void CCell::setCellValue(int num)
{
value = num;
}


I have the height and width set at 20 and 30 respectively. When i change the height to 10, j jumps to 10 immediately too. Ive tried to clean projct then build but nothing. This error came out of nowhere becsuse i have not touched CGrid today for coding and it worked prior but now it doesnt when nothing modifies it.

jefftee
19th July 2014, 06:34
See my edited post above.

bnosam
19th July 2014, 16:16
See my edited post above.

That was exactly it, I know why it just started happening now too. I always had it set to 15 x 15 so the height would always be equal with width so there would be no overflow.

Thanks so much!

anda_skoa
19th July 2014, 16:28
Maybe a stupid question, but the goal seems to be to call setCellValue() on all vector elements.
Why not just iterate over the vector's length?

Cheers,
_

ChrisW67
19th July 2014, 22:10
Or just call QVector::fill()

bnosam
20th July 2014, 02:55
Maybe a stupid question, but the goal seems to be to call setCellValue() on all vector elements.
Why not just iterate over the vector's length?

Cheers,
_


Fair point to make. I'm not the most aware of the vector class or anything in the STL to be honest, I like it but the quick look I did on the doc of it on the Qt site I didn't see that.

I appreciate your recommendation!

anda_skoa
20th July 2014, 09:57
Fair point to make. I'm not the most aware of the vector class or anything in the STL to be honest

Vectors are like arrays, their elements can be accessed using the index operator []. But in contrast to arrays, they also know about their size.



I like it but the quick look I did on the doc of it on the Qt site I didn't see that
QVector::count(), QVector::length() and QVector::size() are basically all equivalent getters for the number of elements in the vector.

Cheers,
_