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

  • SEARCH
  • Home
  • 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

I recently finished a university course in C. Therefore I lack experience, of course.
We've recently finished development of a web application that works as expected. When we
Recently I have been investigating the possibilities of caching in ASP.NET. I rolled my
Recently I had to develop a SharePoint workflow, and I found the experience quite
Recently Jeff has posted regarding his trouble with database deadlocks related to reading. Multiversion
Recently, I started changing some of our applications to support MS SQL Server as
Recently, I've been dealing with an error with accessing MAPI via the .NET framework
Recently our site has been deluged with the resurgence of the Asprox botnet SQL
Recently we got a new server at the office purely for testing purposes. It
Recently, I read an article entitled SATA vs. SCSI reliability . It mostly discusses

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.