Help needed for a iterator mysteriously going out of bounds


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Help needed for a iterator mysteriously going out of bounds

  1. #1
    Join Date
    Apr 2009
    Posts
    2

    Help needed for a iterator mysteriously 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:

    [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 before crashing.

    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.

    Any suggestions are much appreciated.

  2. #2
    Join Date
    Dec 2007
    Posts
    401
    if you erase some item from a standard sequence container, iterators become invalid; so you cannot continue with the loop driven by invalid iterators. specifically, vector<>::erase() invalidates all iterators (and pointers) following the element to be erased.

    in general, erase(iterator) of a sequence container removes the element identified by iterator and returns a valid iterator to the element just after the one erased (or end() if there is no such element) . so, you could do this:

    Code:
    if( CAR_VEC[i].VELOCITY==0 )
    {
        std::vector<int>::iterator STEP = CAR_VEC[i].PASSD.begin() ; 
        while( STEP != CAR_VEC[i].PASSD.end() )
        {
            if ( *STEP==ROAD_STOP[j]->NUM)
            {
                std::cout << "ok- bus:" << i << " pass:" << *STEP << " stop:" << ROAD_STOP[j]->NUM << '\n' ;
                STEP = CAR_VEC[i].PASSD.erase( STEP ) ;
                std::cout << "erased" << '\n' ;
            }
            else ++STEP ;
        }
    }

  3. #3
    Join Date
    Apr 2009
    Posts
    2
    Yeah that fixes it, I was under the impression that iterators solved that problem by themselves, maintaining the order of elements until the changes have been made.

    Thanks for the help

Similar Threads

  1. help needed to work out something
    By jamjas in forum ASP.NET
    Replies: 2
    Last Post: 06-01-2007, 12:49 AM
  2. Getting a GUI to run
    By Eric in forum Java
    Replies: 4
    Last Post: 04-14-2006, 10:09 AM
  3. Display PictureBox outside bounds of a Usercontrol
    By jennyfletcher in forum VB Classic
    Replies: 8
    Last Post: 09-07-2005, 05:59 AM
  4. Replies: 2
    Last Post: 10-11-2002, 02:53 PM
  5. Getting a GUI to function
    By Eric in forum Java
    Replies: 1
    Last Post: 11-27-2001, 07:53 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center