iterator problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chameleon

    #1

    iterator problem

    All these happen in VC++ NET 2003, if you ask me about the compiler.
    it._Myptr is a VC++ specific member of the iterator implementation.
    But from this, we extract usefull informations about crash.
    ----------------------------
    vector<int*> v;
    vector<int*>::i terator it = v.begin(); // it._Myptr == 0 (what pointer
    is this?)
    v.insert(it, 5); // works
    it++; // it._Myptr == 4
    v.insert(it, 5); // crashes
    ----------------------------
    and this.
    ----------------------------
    vector<int*> v;
    vector<int*>::i terator it = v.begin(); // it._Myptr == 0
    v.push_back(5);
    vector<int*>::i terator it = v.begin(); // it._Myptr == 0x00323b40 (the
    pointer to first element)
    ----------------------------


    All of these are compiler bugs on iterator implementation, or I miss
    something?

    Thanks
  • Kai-Uwe Bux

    #2
    Re: iterator problem

    Chameleon wrote:
    [color=blue]
    > All these happen in VC++ NET 2003, if you ask me about the compiler.
    > it._Myptr is a VC++ specific member of the iterator implementation.
    > But from this, we extract usefull informations about crash.
    > ----------------------------
    > vector<int*> v;
    > vector<int*>::i terator it = v.begin();
    > // it._Myptr == 0 (what pointer is this?)
    > v.insert(it, 5); // works[/color]

    This insert() call invalidates all iterators to v. In particular:
    [color=blue]
    > it++; // it._Myptr == 4[/color]

    is undefined behavior,
    [color=blue]
    > v.insert(it, 5); // crashes[/color]

    which manifests itself here.
    [color=blue]
    > ----------------------------
    > and this.
    > ----------------------------
    > vector<int*> v;
    > vector<int*>::i terator it = v.begin(); // it._Myptr == 0
    > v.push_back(5);[/color]

    Do you think that 5 is a good int* ?
    [color=blue]
    > vector<int*>::i terator it = v.begin(); // it._Myptr == 0x00323b40 (the
    > pointer to first element)
    > ----------------------------[/color]

    What about it?

    [color=blue]
    > All of these are compiler bugs on iterator implementation, or I miss
    > something?[/color]

    You missed the rules for iterators for std::vector.



    Best

    Kai-Uwe Bux

    Comment

    • Chameleon

      #3
      Re: iterator problem

      Kai-Uwe Bux wrote:[color=blue]
      > Chameleon wrote:
      >[color=green]
      >> All these happen in VC++ NET 2003, if you ask me about the compiler.
      >> it._Myptr is a VC++ specific member of the iterator implementation.
      >> But from this, we extract usefull informations about crash.
      >> ----------------------------
      >> vector<int*> v;
      >> vector<int*>::i terator it = v.begin();
      >> // it._Myptr == 0 (what pointer is this?)
      >> v.insert(it, 5); // works[/color]
      >
      > This insert() call invalidates all iterators to v. In particular:
      >[color=green]
      >> it++; // it._Myptr == 4[/color]
      >
      > is undefined behavior,
      >[color=green]
      >> v.insert(it, 5); // crashes[/color]
      >
      > which manifests itself here.
      >[color=green]
      >> ----------------------------
      >> and this.
      >> ----------------------------
      >> vector<int*> v;
      >> vector<int*>::i terator it = v.begin(); // it._Myptr == 0
      >> v.push_back(5);[/color]
      >
      > Do you think that 5 is a good int* ?[/color]


      sorry, it is a fast-written sample:
      vector<int> v;

      Comment

      Working...