Incorrect vector subscript out of range

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sir G
    New Member
    • Sep 2011
    • 7

    Incorrect vector subscript out of range

    Hi all,
    I'm working in visual studio 2010. I'm getting a "vector subscript out of range debug assertion check" -error, which I believe is incorrect.

    I have created a minimal example:

    Code:
    int xsize = 4dVector.size();
    int ysize = 4dVector[x].size();
    int zsize = 4dVector[x][y].size();
    int nbelements = 4dVector[x][y][z].size();
    
    	for (uint32_t i = 0; i < 4dVector[x][y][z].size(); i++) {		
    		Foo* foo = 4dVector[x][y][z][i];
    		
    		// do something
    	}
    The debugger shows the out-of-range message at line:
    Foo* foo = 4dVector[x][y][z][i];

    however, when inspecting all variables I find:

    xsize = 20;
    ysize = 20;
    zsize = 20;
    nbelements = 17;

    x = 11;
    y = 17;
    z = 14;
    i = 14;

    I don't see the problem with that?
    Also, every time I rerun the program in debugging mode, it breaks with different values in all variables, but always correct values. All help is much appreciated.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Are all of your vector eements initialized? The out-of-range error is common with usng uninitialized vectors.

    Code:
    vector<int> arr;
    arr[5] = 10;     //subscript out-of-range
    cout << arr[0];  //subscript out-of-range

    Comment

    • Sir G
      New Member
      • Sep 2011
      • 7

      #3
      Yes they all were initialized. I have found the problem though. It was a specific concurrency related issue of the program.

      Thanks for all help though

      Comment

      Working...