Vector subscript out of range- error whilst debugging
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: Vector subscript out of range- error whilst debugging

  1. #1
    Join Date
    Apr 2014
    Posts
    17

    Vector subscript out of range- error whilst debugging

    I have the follwoing code. however, when I debug it gives an error saying" vector subscript out of range"

    Can anyone pelase help?

    //Vector based multi-dimensional arrays
    //Vectors are a STL container that allow you to store pretty much anything in them. When used correctly they can be very powerful containers.
    //
    //They provide an added benefit that they will automatically remove the memory they use when they go out of scope.
    //
    //This means that objects stored within a vector do not need to be de-allocated (but pointers to objects do).
    //
    //You can also do some interesting things with dynamic multi-dimensional arrays with vectors.
    //
    //For example, if you only allocate the first dimension, then use the .push_back() to add records to the 2nd dimension it's no longer a grid,
    //
    //but an array with a dynamically sized 2nd dimension (much like a street of buildings each with a different amount of floors).
    //
    //This functionality can be achieved using pointers, but is much harder to do.




    #include <iostream>
    #include <vector>
    #include<conio.h>


    using std::vector;
    using namespace std;

    #define HEIGHT 5
    #define WIDTH 3
    #define DEPTH 7

    int main() {
    vector<vector<vector<double> > > array3D;

    // Set up sizes. (HEIGHT x WIDTH)
    array3D.resize(HEIGHT);
    for (int i = 0; i < HEIGHT; ++i) {
    array3D[i].resize(WIDTH);

    for (int j = 0; j < WIDTH; ++j)
    array3D[i][j].resize(DEPTH);


    // Put some values in
    array3D[1][2][1] = 6.0;
    array3D[2][1][2] = 5.5;


    }


    // for (int i = 0; i < HEIGHT; ++i)
    // {
    //
    // for (int j = 0; j < WIDTH; ++j)
    //
    // {
    // for (int k = 0; k < DEPTH; ++k)
    // {
    //
    // //cout<<"array3D "<<"["<<i<<"]"<<"["<<j<<"]"<<"["<<k<<"]"<<"= "<<array3D[i][j][k];
    //
    // getch();
    //
    //
    // }
    // }
    //}


    }

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,352

    Re: Vector subscript out of range- error whilst debugging

    Quote Originally Posted by atee View Post
    I have the follwoing code. however, when I debug it gives an error saying" vector subscript out of range"

    Can anyone pelase help?
    You have to debug your code to find out the reason of the problem.
    Besides, please always use CODE tags while posting code snippets.
    Victor Nijegorodov

  3. #3
    Join Date
    Apr 2014
    Posts
    17

    Re: Vector subscript out of range- error whilst debugging

    It doesn't allow me to debug.

    When I debug it says debug assertion failed with a message stating "vector subsript out of range"

    I have wrapped the code below.

    Thanks for your help


    Code:
    //Vector based multi-dimensional arrays
    //Vectors are a STL container that allow you to store pretty much anything in them. When used correctly they can be very powerful containers.
    //
    //They provide an added benefit that they will automatically remove the memory they use when they go out of scope. 
    //
    //This means that objects stored within a vector do not need to be de-allocated (but pointers to objects do).
    //
    //You can also do some interesting things with dynamic multi-dimensional arrays with vectors. 
    //
    //For example, if you only allocate the first dimension, then use the .push_back() to add records to the 2nd dimension it's no longer a grid, 
    //
    //but an array with a dynamically sized 2nd dimension (much like a street of buildings each with a different amount of floors). 
    //
    //This functionality can be achieved using pointers, but is much harder to do.
    
    
    
    
    #include <iostream>
    #include <vector>
    #include<conio.h>
    
    
    using std::vector;
    using namespace std;
    
    #define HEIGHT 5
    #define WIDTH 3
    #define DEPTH 7
    
    int main() {
    vector<vector<vector<double> > > array3D;
    
    // Set up sizes. (HEIGHT x WIDTH)
    array3D.resize(HEIGHT);
    for (int i = 0; i < HEIGHT; ++i) {
    array3D[i].resize(WIDTH);
    
    for (int j = 0; j < WIDTH; ++j)
    array3D[i][j].resize(DEPTH);
    
    
    // Put some values in
    array3D[1][2][1] = 6.0;
    array3D[2][1][2] = 5.5;
    
    
    } 
    
    
    // for (int i = 0; i < HEIGHT; ++i)
    // {
    //
    // for (int j = 0; j < WIDTH; ++j)
    //
    // {
    // for (int k = 0; k < DEPTH; ++k)
    // {
    //
    // //cout<<"array3D "<<"["<<i<<"]"<<"["<<j<<"]"<<"["<<k<<"]"<<"= "<<array3D[i][j][k];
    //
    // getch();
    //
    //
    // }
    // }
    //}
    
    
    }

  4. #4
    Join Date
    Aug 1999
    Location
    Darmstadt, FRG
    Posts
    86

    Re: Vector subscript out of range- error whilst debugging

    As far as I understood your code you forgot to set the size of the DEPTH-dimension properly:

    Instead of:
    Code:
    int main() {
    vector<vector<vector<double> > > array3D;
    
    // Set up sizes. (HEIGHT x WIDTH)
    array3D.resize(HEIGHT);
    for (int i = 0; i < HEIGHT; ++i) {
    array3D[i].resize(WIDTH);
    
    for (int j = 0; j < WIDTH; ++j)
    array3D[i][j].resize(DEPTH);
    
    // Put some values in
    array3D[1][2][1] = 6.0;
    array3D[2][1][2] = 5.5;
    }
    the following code should do (mind the braces, i.e. the second for-loop must be controlled by the first one):
    Code:
    int main() 
    {
    vector<vector<vector<double> > > array3D;
    
    // Set up sizes. (HEIGHT x WIDTH* DEPTH)
    array3D.resize(HEIGHT);
    for (int i = 0; i < HEIGHT; ++i)
        {
        array3D[i].resize(WIDTH);
        for (int j = 0; j < WIDTH; ++j)
            {
            array3D[i][j].resize(DEPTH);
            }
        }
    
    // Put some values in
    array3D[1][2][1] = 6.0;
    array3D[2][1][2] = 5.5;
    }

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,067

    Re: Vector subscript out of range- error whilst debugging

    Quote Originally Posted by atee View Post
    It doesn't allow me to debug.

    When I debug it says debug assertion failed with a message stating "vector subsript out of range"
    You can always debug. Put a breakpoint in the first line of executable code in your program, then step until you get the error, or when you get the ASSERT, click the retry button. Look at the locals to see what the subscript is. Use the call stack to get back to your last line of code if you have to.

  6. #6
    Join Date
    Aug 1999
    Location
    Darmstadt, FRG
    Posts
    86

    Re: Vector subscript out of range- error whilst debugging

    Besides the initialization-error in your code (see #4) there is also a major design-mistake: If you use nested vectors, you'll loose most of the efficiency a vector provides. You may have a look to TC++PL, 4th ed, Chpt: 31.4.1.2

  7. #7
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,827

    Re: Vector subscript out of range- error whilst debugging

    note that while you can make a 3D array by nesting 3 vectors, it's a very suboptimal way to do so. you'll have memory management for each dimension which is why you need the "set up sizes" loops.

    even if you really need all 3 dimensions to be dynamically defined it's still not optimal.

    for fixed sizes, you're better off creating your own 3D array templateclass (without all the dynamic sizing support)
    something like:

    for dynamic sizes, you can create a class around a a single vector<double> that allocates height*width*depth elements and do all internal element calculations yourself.

    for educational purposes the nested vectors will work well enough.
    if you need this as part of an elaborate class design in a "complex" application, the nested vectors will probably cause quite a bit of unnecessary overhead.

  8. #8
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,316

    Re: Vector subscript out of range- error whilst debugging

    TC++PL
    ??
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  9. #9
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,261

    Re: Vector subscript out of range- error whilst debugging

    Stroustrup's book, The C++ Programming Language
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center