Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 896283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:42:10+00:00 2026-05-15T14:42:10+00:00

I recently finished fixing a bug in the following function, and the answer surprised

  • 0

I recently finished fixing a bug in the following function, and the answer surprised me. I have the following function (written as it was before I found the bug):

    void Level::getItemsAt(vector<item::Item>& vect, const Point& pt)
    {
        vector<itemPtr>::iterator it; // itemPtr is a typedef for a std::tr1::shared_ptr<item::Item>
        for(it=items.begin(); it!=items.end(); ++it)
        {
            if((*it)->getPosition() == pt)
            {
                item::Item item(**it);
                items.erase(it);
                vect.push_back(item);
            }
        }
    }

This function finds all Item objects in the ‘items’ vector that has a certain position, removes them from ‘items’, and puts them in ‘vect’. Later, a function named putItemsAt does the opposite, and adds items to ‘items’. The first time through, getItemsAt works fine. After putItemsAt is called, though, the for loop in getItemsAt will run off the end of ‘items’. ‘it’ will point at an invalid Item pointer, and getPosition() segfaults. On a hunch, I changed it!=items.end() to it<items.end(), and it worked. Can anyone tell me why? Looking around SO suggests it might involve erase invalidating the iterator, but it still doesn’t make sense why it would work the first time through.

I’m also curious because I plan to change ‘items’ from a vector to a list, since list’s erase is more efficient. I know I’d have to use != for a list, as it doesn’t have a < operator. Would I run into the same problem using a list?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T14:42:10+00:00Added an answer on May 15, 2026 at 2:42 pm

    When you call erase(), that iterator becomes invalidated. Since that is your loop iterator, calling the ‘++’ operator on it after invalidating it is undefined behavor. erase() returns a new valid iterator that points to the next item in the vector. You need to use that new iterator from that point onwards in your loop, ie:

    void Level::getItemsAt(vector<item::Item>& vect, const Point& pt) 
    { 
        vector<itemPtr>::iterator it = items.begin();
        while( it != items.end() )
        {
            if( (*it)->getPosition() == pt )
            {
                item::Item item(**it);
                it = items.erase(it);
                vect.push_back(item);
            }
            else
                ++it;
        } 
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Recently I have finished my first Iphone app and it works grate.... except for
I recently finished a class that we're using to tie Access to some WCF
I just recently finished reading Secure Coding in C and C++ by Brian Seacord,
I'm very new to Ruby. I've recently finished writing a simple script, which uses
Recently I just finished a simple Facebook Air application, and some of my friends
I recently forced myself to study C++ and I just finished reading the book
I just finished what I would call a small django project and pretty soon
I've just recently learned C# and am interested in .net but I don't know
I am using the Valums Ajax Uploader to upload a batch of files. We
I'm using Qt 4.7.0 (32 bit) on Windows 7 Ultimate (32 bit) machine. I've

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.