Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


#ActualÁlvaro

Posted 06 May 2013 - 06:02 AM


//THIS DELETES THE REDUNDANT BULLETS
void handleProjectiles()
{
   if( !allProjectiles.empty() )
   {  
      std::vector<projectile>::iterator it = allProjectiles.begin();
     
      for( ; it != allProjectiles.end(); it++ )
      {
           it->x += it->velocity;    
           it->show();
           
           if( it->x + it->w < 0 )
           {
               allProjectiles.erase( allProjectiles.begin(), it );
           }
      }
   }
}


 
That code is messed up. Whenever you find a bullet that satisfies certain conditions, you erase all the previous bullets in the vector. This will move all the remaining bullets to the beginning of the vector, and it will invalidate the iterator `it' (in a common implementation vector iterators are pointers, and you now have a pointer possibly pointing past the end of the vector, and now you won't hit the loop's terminating condition ever, so eventually it will point past the allocated block).

A clean solution to this problem is the erase-remove idiom.

#2Álvaro

Posted 06 May 2013 - 06:01 AM

//THIS DELETES THE REDUNDANT BULLETS
void handleProjectiles()
{
   if( !allProjectiles.empty() )
   {  
      std::vector<projectile>::iterator it = allProjectiles.begin();
     
      for( ; it != allProjectiles.end(); it++ )
      {
           it->x += it->velocity;    
           it->show();
           
           if( it->x + it->w < 0 )
           {
               allProjectiles.erase( allProjectiles.begin(), it );
           }
      }
   }
}

 

That code is messed up. Whenever you find a bullet that satisfies certain conditions, you erase all the previous bullets in the vector. This will move all the remaining bullets to the beginning of the vector, and it will invalidate the iterator `it' (in a common implementation vector iterators are pointers, and you now have a pointer possibly pointing past the end of the vector, and now you won't hit the loop's terminating condition ever, so eventually it will point past the allocated block).

A clean solution to this problem is the http://en.wikipedia.org/wiki/Erase-remove_idiom


#1Álvaro

Posted 06 May 2013 - 06:00 AM

//THIS DELETES THE REDUNDANT BULLETS
void handleProjectiles()
{
   if( !allProjectiles.empty() )
   {  
      std::vector<projectile>::iterator it = allProjectiles.begin();
     
      for( ; it != allProjectiles.end(); it++ )
      {
           it->x += it->velocity;    
           it->show();
           
           if( it->x + it->w < 0 )
           {
               allProjectiles.erase( allProjectiles.begin(), it );
           }
      }
   }
}

 

That code is messed up. Whenever you find a bullet that satisfies certain conditions, you erase all the bullets from the *previous* bullets in the vector. This will move all the remaining bullets to the beginning of the vector, and it will invalidate the iterator `it' (in a common implementation vector iterators are pointers, and you now have a pointer possibly pointing past the end of the vector, and now you won't hit the loop's terminating condition ever, so eventually it will point past the allocated block).

A clean solution to this problem is the http://en.wikipedia.org/wiki/Erase-remove_idiom


PARTNERS