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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:23:55+00:00 2026-05-27T12:23:55+00:00

I was playing around with some code today, and was very surprised to find

  • 0

I was playing around with some code today, and was very surprised to find out that the following code crashes in Visual C++. Does it look OK? gcc prints out 1 when I run the code. So does Intel.

#include <iostream>
#include <set>
#include <assert.h>

int main()
{
    std::set<int> sampleSet;
    sampleSet.insert(1);
    sampleSet.insert(2);
    sampleSet.insert(3);
    std::set<int>::iterator normalIt(sampleSet.begin());
    std::set<int>::reverse_iterator reverseIt(sampleSet.rbegin());

    ++normalIt;
    ++reverseIt;

    int test1(*reverseIt); // 2
    assert(*normalIt == *reverseIt); //they're both = 2
    std::set<int>::iterator gonnaDelete((++reverseIt).base()); // gonnaDelete points to 2
    int test2(*reverseIt); // 1
    sampleSet.erase(gonnaDelete);
    int test3(*reverseIt); // 1???   Visual Studio 2010 crashes here.... gcc is fine, but not sure if this is legal
    std::cout << test3 << std::endl;

    return 0;
}
  • 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-27T12:23:56+00:00Added an answer on May 27, 2026 at 12:23 pm

    It’s not legal. According to [lib.associative.reqmts] in C++98, “the erase members shall invalidate only iterators and references to the erased elements.” Since reverse_iterator is defined something like:

    template<typename Iter>
    class reverse_iterator {
      Iter current;
      Iter tmp;
     public:
      ...
      Iter base() { return current; }
      reference operator*() {
        tmp = current;
        --tmp;
        return *tmp;
      }
    };
    

    when you erase gonnaDelete you also invalidate reverseIt.current, so when you subsequently dereference reverseIt, you get undefined behavior.

    Now, set iterators tend to be simple pointers to nodes, and optimized allocators sometimes leave the memory of a deleted node alone, so the code sometimes happens to do what you expect even after you’ve invalidated one of them, but it’s not at all guaranteed to work. I suspect VC++ has a debugging mode on by default to catch this kind of mistake. To turn on gcc’s equivalent mode, try building with -D_GLIBCXX_DEBUG: http://gcc.gnu.org/onlinedocs/gcc-4.6.2/libstdc++/manual/manual/bk01pt03ch17s03.html

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

Sidebar

Related Questions

I was playing around with some code today, and I came across the idea
I have been playing around with some debugging and wrote some C code that
This surprised me a little bit, but I was playing around with some code
Here is some C++ code I'm playing around with: #include <iostream> #include <vector> #define
I've playing around with linux and noticed that for some mysterious reason commands like
I was just playing around with some code in LINQPad and managed to crash
I'm just playing around with some code. I create an Activity and simply do
I was playing around with some code when I noticed something strange: [~] main%
I'm playing around with some code katas and trying to get a better understanding
I'm playing around, trying to write some code to use the tr.im APIs to

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.