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 8829553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:51:35+00:00 2026-06-14T07:51:35+00:00

Possible Duplicate: Problem with std::map::iterator after calling erase() May be you could enlighten me

  • 0

Possible Duplicate:
Problem with std::map::iterator after calling erase()

May be you could enlighten me with an issue I’m currently having.
So problem is that I get a bad access memory when I’m trying to erase some elements in the map.
We suppose the following typedef :

typedef std::map < std::string *, Document *, pStringCompare > Map;

And we suppose we already have instantiated a map which contains two elements (for example) before the execution of all the following code.
This code with working great :

Map::iterator it = documents.begin();

std::string *s = it->first;
Document *d = it->second;

documents.erase(it);

delete d;
delete s;

But when I’m trying to loop with the iterator, i get the error.

for (Map::iterator it = documents.begin() ; it != documents.end() ; it++)
{
    std::string s = * ( it->first);
    Document dd = * (it->second);
    std::cout << s << " || " << dd;
    documents.erase(it);  // This line causes the bad access memory error.
}

Thanks for your help ! It’s really appreciated !

Aleks

  • 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-06-14T07:51:36+00:00Added an answer on June 14, 2026 at 7:51 am

    You are accessing invalidated iterator. Once you pass it to erase() it is no longer valid, so you cannot increment it in your for loop. See Iterator Invalidation Rules for more details on that.

    Your loop should be structured like this to avoid accessing invalidated iterators:

    for (Map::iterator it = documents.begin() ; it != documents.end() ; )
    {
        std::string s = * ( it->first);
        Document dd = * (it->second);
        std::cout << s << " || " << dd;
        documents.erase(it++);
    }
    

    Though I really recommend you read more documentation and understand what is going on here. For example, see this.

    Good Luck!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: C++ std::string conversion problem on Windows How to convert std::string to LPCSTR?
Possible Duplicate: C# serialport and hyperterminal Problem I'm having trouble with my serial connection.
Possible Duplicate: C++ overload resolution I ran into a problem where after my class
Possible Duplicate: Css z-index problem I have an issue where one image is infront
Possible Duplicate: C++ cin whitespace question I'm having problem trying to understand this piece
Possible Duplicate: Access issue regarding static variable I'm having what seems to be a
Possible Duplicate: Floating point inaccuracy examples Im having a problem... When i compile the
Possible Duplicate: Problem calling a Local HTML File from XCode I am using the
Possible Duplicate: Problem executing bash file I have created a bash file script and
Possible Duplicate: Problem with database in CodeIgniter Alright, I've googled this the last two

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.