Results 1 to 9 of 9

Thread: QVector issue going out of bounds

  1. #1
    Join Date
    Jun 2014
    Posts
    13
    Thanks
    1
    Qt products
    Qt5

    Default QVector issue going out of bounds

    Whenever I run my code I get an error stating the vector went out of bounds. The vector is declared as

    Qt Code:
    1. QVector <CCell> grid;
    To copy to clipboard, switch view to plain text mode 
    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
    Qt Code:
    1. resize(width * height);
    To copy to clipboard, switch view to plain text mode 

    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:

    Qt Code:
    1. void CGrid::clearMap()
    2. {
    3. int i = 0;
    4. int j = 0;
    5.  
    6. for(i = 0; i < width; i++)
    7. {
    8. for(j = 0; j < height; j++)
    9. {
    10. grid[i * width + j].setCellVal(0);
    11. }
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 

    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.

  2. #2
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: QVector issue going out of bounds

    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:

    Qt Code:
    1. void CGrid::clearMap()
    2. {
    3. int i = 0;
    4. int j = 0;
    5.  
    6. for(i = 0; i < width; i++)
    7. {
    8. for(j = 0; j < height; j++)
    9. {
    10. grid[i * height + j].setCellVal(0);
    11. }
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 
    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:
    Qt Code:
    1. int index = i * height + j;
    2. grid[index].setCellVal(0);
    To copy to clipboard, switch view to plain text mode 
    The above would have been much easier to spot IMHO...
    Last edited by jefftee; 19th July 2014 at 05:31.

  3. #3
    Join Date
    Jun 2014
    Posts
    13
    Thanks
    1
    Qt products
    Qt5

    Default Re: QVector issue going out of bounds

    Setcellvalue is just basically
    Qt Code:
    1. void CCell::setCellValue(int num)
    2. {
    3. value = num;
    4. }
    To copy to clipboard, switch view to plain text mode 


    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.

  4. #4
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: QVector issue going out of bounds

    See my edited post above.

  5. #5
    Join Date
    Jun 2014
    Posts
    13
    Thanks
    1
    Qt products
    Qt5

    Default Re: QVector issue going out of bounds

    Quote Originally Posted by jthomps View Post
    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!

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QVector issue going out of bounds

    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,
    _

  7. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QVector issue going out of bounds

    Or just call QVector::fill()

  8. #8
    Join Date
    Jun 2014
    Posts
    13
    Thanks
    1
    Qt products
    Qt5

    Default Re: QVector issue going out of bounds

    Quote Originally Posted by anda_skoa View Post
    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!

  9. #9
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QVector issue going out of bounds

    Quote Originally Posted by bnosam View Post
    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.

    Quote Originally Posted by bnosam View Post
    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,
    _

Similar Threads

  1. sort a Qvector based on another Qvector
    By OzQTNoob in forum Qt Programming
    Replies: 2
    Last Post: 16th February 2012, 06:39
  2. Replies: 5
    Last Post: 2nd September 2011, 23:11
  3. Replies: 1
    Last Post: 5th February 2011, 03:04
  4. Out of bounds Qlist
    By seink in forum Newbie
    Replies: 2
    Last Post: 22nd November 2010, 15:31
  5. Help with out of bounds message
    By aarelovich in forum Qt Programming
    Replies: 15
    Last Post: 20th February 2009, 10:44

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.