Thread: Help needed with an iterator going out of bounds

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    1

    Help needed with an iterator going out of bounds

    I'm writing a program that simulates road traffic, I have a vector of integers inside a structure, which is itself the elements of a vector. Everything works fine except the iterator i use to delete elements of the inbedded integer vector seems to be going out bound and then when it trys to delete the element the program crashes.

    The code that does this is here:

    Code:
    if (CAR_VEC[i].VELOCITY==0){ 
    													vector<int>::iterator STEP;
    													for (STEP=CAR_VEC[i].PASSD.begin(); STEP!=CAR_VEC[i].PASSD.end(); STEP++){
    						
    														if ( *STEP==ROAD_STOP[j]->NUM){
    															cout <<"ok- bus:"<<i<<" pass:"<< *STEP<<" stop:"<<ROAD_STOP[j]->NUM<<endl;
    															CAR_VEC[i].PASSD.erase(STEP); //something wrong with this line
    															cout <<"erased"<<endl;}}
    Where CAR_VEC is [vector < CARS > CAR_VEC;], cars being the struct which PASSD is inside.

    The cout's are just so I can check that the integers I want are sensible, which they are. I've tried everything and nailed down the problem to something being deleted that isn't there. Yet I can't see why the iterator is going out of bounds. Is it something to do with the vector being inbedded in a structure, in which case do I need to declare the iterator as part of the larger vector CAR_VEC.

    ie [ CAR_VEC[i].vector<int>::iterator STEP;] although I've tried this and it doesn't compile.

    this is the output it gives

    Code:
    bus:21
    2
    2
    2
    0
    0
    0
    2
    
    ok- bus:21 pass:2 stop:2
    erased
    ok- bus:21 pass:2 stop:2
    erased
    ok- bus:21 pass:2 stop:2
    erased
    ok- bus:21 pass:2 stop:2
    
    Passenger_vector has exited due to signal 11 (SIGSEGV).
    Strangely it works fine as long as the number being deleted isn't the last one on the list.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    That's because erase invalidates the iterator and also makes the vector shorter, so if you increment the iterator blindly, you can well walk over the end of the vector.

    Erasing from the container while iterating it is tricky but it looks basically like this:

    Code:
    for (iterator it = container.begin(); it != container.end; ) {
        if (condition) {
            it = container.erase(it);
        }
        else {
            ++it;
        }
    }
    Another, and probably better option, would be to create a function object and use the std::remove_if algorithm to do the dirty work.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Going beyond bounds problem
    By swgh in forum C++ Programming
    Replies: 4
    Last Post: 01-03-2009, 10:41 AM
  2. Array bounds checking
    By rohan_ak1 in forum C Programming
    Replies: 2
    Last Post: 08-26-2008, 10:16 PM
  3. out of bounds problem
    By chris285 in forum Game Programming
    Replies: 1
    Last Post: 04-26-2005, 09:00 AM
  4. C++ help needed
    By Enkindu in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 08-31-2004, 11:24 PM
  5. Game matrix bounds checking
    By Panopticon in forum Game Programming
    Replies: 2
    Last Post: 02-04-2003, 10:30 PM