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
  • Calum Grant

    #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?)[/color]

    The same as v.end(). No bug. You're not going to dereference it.
    [color=blue]
    > v.insert(it, 5); // works[/color]

    That's okay, but invalidates "it".
    [color=blue]
    > it++; // it._Myptr == 4[/color]

    Unfortunately, "it" is invalid.
    [color=blue]
    > v.insert(it, 5); // crashes[/color]

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

    This compiles?
    [color=blue]
    > 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?[/color]

    Ah, not every error is a compiler error!

    Cheers, Calum

    Comment

    • Daniel T.

      #3
      Re: iterator problem

      In article <ds38pl$jpi$1@v olcano1.grnet.g r>,
      Chameleon <cham_gss@hotma il.NOSPAM.com> wrote:
      [color=blue]
      > All of these are compiler bugs on iterator implementation, or I miss
      > something?[/color]

      Until you know enough that you can build your own compiler, go ahead and
      assume that the bug is in your code rather than the compiler's code...
      Just a thought.


      --
      Magic depends on tradition and belief. It does not welcome observation,
      nor does it profit by experiment. On the other hand, science is based
      on experience; it is open to correction by observation and experiment.

      Comment

      • Chameleon

        #4
        Re: iterator problem

        Calum Grant 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?)[/color]
        >
        > The same as v.end(). No bug. You're not going to dereference it.
        >[color=green]
        >> v.insert(it, 5); // works[/color]
        >
        > That's okay, but invalidates "it".
        >[color=green]
        >> it++; // it._Myptr == 4[/color]
        >
        > Unfortunately, "it" is invalid.
        >[color=green]
        >> v.insert(it, 5); // crashes[/color]
        >
        > Yup.
        >[color=green]
        >> ----------------------------
        >> and this.
        >> ----------------------------
        >> vector<int*> v;
        >> vector<int*>::i terator it = v.begin(); // it._Myptr == 0
        >> v.push_back(5);[/color]
        >
        > This compiles?[/color]

        sorry, vector<int> v;
        [color=blue][color=green]
        >> 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?[/color]
        >
        > Ah, not every error is a compiler error![/color]


        of-course!

        my solution is this until now:

        -------------------------------------
        vector<int*> v;
        vector<int*>::i terator it = v.begin();
        it = v.insert(it, 5);
        it++;
        it = v.insert(it, 5);
        -------------------------------------

        Comment

        • Luke Meyers

          #5
          Re: iterator problem

          Chameleon wrote:[color=blue][color=green][color=darkred]
          > >> vector<int*> v;
          > >> vector<int*>::i terator it = v.begin(); // it._Myptr == 0
          > >> v.push_back(5);[/color]
          > >
          > > This compiles?[/color]
          >
          > sorry, vector<int> v;[/color]

          Okay, vector<int>. Good.
          [color=blue]
          > my solution is this until now:
          >
          > -------------------------------------
          > vector<int*> v;
          > vector<int*>::i terator it = v.begin();
          > it = v.insert(it, 5);
          > it++;
          > it = v.insert(it, 5);
          > -------------------------------------[/color]

          Back to vector<int*> now? Well, in any case... line 2 above serves no
          purpose. I would change this snippet to:

          std::vector<int > v;
          v.push_back(5);
          v.push_back(5);

          See? No need for iterators at all in this case. Maybe you got
          confused because you didn't know about push_back? You owe it to
          yourself to learn the public interface of important classes like
          std::vector. It is only wafer-thin.

          Luke

          Comment

          Working...