std::vector - bug or feature?

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

    std::vector - bug or feature?

    hi ng,

    i'm working on a multiplayer game for a variable number of players and on the client side, i'm using a std::vector<CPl ayer> to store
    informatik about the players. CPlayer is a class that contains another std::vector<CPo sition>. Because one of the players is the
    client itself (and the size of the vector<CPlayer> doesn't change during a game), i thought i could store a
    std::vector<CPl ayer>::iterator "localplaye r" that points to the respective element of the vector.
    The strange thing is that when i add elements to the std::vector<CPo sition> (which is nothing more than a usual member of CPlayer)
    the iterator "localplaye r" becomes somehow invalid (in a way that the memory it points to is no longer the actual "localplaye r" but
    rather some random position in memory). what's wrong here? i didn't change anything about the std::vector<CPl ayer> so why can't i
    use the "localplaye r" iterator any more? and: would it help to store a reference to the element rather than an iterator? i fixed
    this problem by using an index instead of iterator, but i'm really curious about this.

    thanks in advance
    janina


  • Leor Zolman

    #2
    Re: std::vector - bug or feature?

    On Sun, 11 Apr 2004 16:12:40 +0200, "Janina Kramer"
    <j.kramer@schoo l-sucks.com> wrote:
    [color=blue]
    >hi ng,
    >
    >i'm working on a multiplayer game for a variable number of players and on the client side, i'm using a std::vector<CPl ayer> to store
    >informatik about the players. CPlayer is a class that contains another std::vector<CPo sition>. Because one of the players is the
    >client itself (and the size of the vector<CPlayer> doesn't change during a game), i thought i could store a
    >std::vector<CP layer>::iterato r "localplaye r" that points to the respective element of the vector.
    >The strange thing is that when i add elements to the std::vector<CPo sition> (which is nothing more than a usual member of CPlayer)
    >the iterator "localplaye r" becomes somehow invalid (in a way that the memory it points to is no longer the actual "localplaye r" but
    >rather some random position in memory). what's wrong here? i didn't change anything about the std::vector<CPl ayer> so why can't i
    >use the "localplaye r" iterator any more? and: would it help to store a reference to the element rather than an iterator? i fixed
    >this problem by using an index instead of iterator, but i'm really curious about this.[/color]

    Offhand, what you've described doesn't seem like it should be invalidating
    that iterator all by itself. Perhaps the iterator is just getting corrupted
    by some bug in your program (e.g., assignment using an out-of-bounds vector
    index)?
    -leor
    [color=blue]
    >
    >thanks in advance
    >janina
    >[/color]

    --
    Leor Zolman --- BD Software --- www.bdsoft.com
    On-Site Training in C/C++, Java, Perl and Unix
    C++ users: Download BD Software's free STL Error Message Decryptor at:
    An STL Error Decryptor for C++ by Leor Zolman of BD Software - available to download here

    Comment

    • Leor Zolman

      #3
      Re: std::vector - bug or feature?

      On Sun, 11 Apr 2004 16:12:40 +0200, "Janina Kramer"
      <j.kramer@schoo l-sucks.com> wrote:
      [color=blue]
      >hi ng,
      >
      >i'm working on a multiplayer game for a variable number of players and on the client side, i'm using a std::vector<CPl ayer> to store
      >informatik about the players. CPlayer is a class that contains another std::vector<CPo sition>. Because one of the players is the
      >client itself (and the size of the vector<CPlayer> doesn't change during a game), i thought i could store a
      >std::vector<CP layer>::iterato r "localplaye r" that points to the respective element of the vector.
      >The strange thing is that when i add elements to the std::vector<CPo sition> (which is nothing more than a usual member of CPlayer)
      >the iterator "localplaye r" becomes somehow invalid (in a way that the memory it points to is no longer the actual "localplaye r" but
      >rather some random position in memory). what's wrong here? i didn't change anything about the std::vector<CPl ayer> so why can't i
      >use the "localplaye r" iterator any more? and: would it help to store a reference to the element rather than an iterator? i fixed
      >this problem by using an index instead of iterator, but i'm really curious about this.[/color]

      Offhand, what you've described doesn't seem like it should be invalidating
      that iterator all by itself. Perhaps the iterator is just getting corrupted
      by some bug in your program (e.g., assignment using an out-of-bounds vector
      index)?
      -leor
      [color=blue]
      >
      >thanks in advance
      >janina
      >[/color]

      --
      Leor Zolman --- BD Software --- www.bdsoft.com
      On-Site Training in C/C++, Java, Perl and Unix
      C++ users: Download BD Software's free STL Error Message Decryptor at:
      An STL Error Decryptor for C++ by Leor Zolman of BD Software - available to download here

      Comment

      • Nick Hounsome

        #4
        Re: std::vector - bug or feature?


        "Janina Kramer" <j.kramer@schoo l-sucks.com> wrote in message
        news:c5bjos$1su $04$1@news.t-online.com...[color=blue]
        > hi ng,
        >
        > i'm working on a multiplayer game for a variable number of players and on[/color]
        the client side, i'm using a std::vector<CPl ayer> to store[color=blue]
        > informatik about the players. CPlayer is a class that contains another[/color]
        std::vector<CPo sition>. Because one of the players is the[color=blue]
        > client itself (and the size of the vector<CPlayer> doesn't change during a[/color]
        game), i thought i could store a[color=blue]
        > std::vector<CPl ayer>::iterator "localplaye r" that points to the respective[/color]
        element of the vector.[color=blue]
        > The strange thing is that when i add elements to the[/color]
        std::vector<CPo sition> (which is nothing more than a usual member of
        CPlayer)[color=blue]
        > the iterator "localplaye r" becomes somehow invalid (in a way that the[/color]
        memory it points to is no longer the actual "localplaye r" but[color=blue]
        > rather some random position in memory). what's wrong here? i didn't change[/color]
        anything about the std::vector<CPl ayer> so why can't i[color=blue]
        > use the "localplaye r" iterator any more? and: would it help to store a[/color]
        reference to the element rather than an iterator? i fixed[color=blue]
        > this problem by using an index instead of iterator, but i'm really curious[/color]
        about this.[color=blue]
        >
        > thanks in advance
        > janina[/color]

        I'm afraid that you must have screwed up somewhere because what you say
        sounds OK and
        I'm willing to bet a lot of money that there is no bug like that which you
        describe in std::vector.

        Are you sure that the size of the CPlayer vector doesn't change?
        How do you create it and how do you add players?
        The only reasonables way to do what you say are:

        std::vector<CPl ayer> players;
        players.reserve (MAX_PLAYERS);
        for up to MAX_PLAYERS
        players.push_ba ck(player);

        or

        std::vector<CPl ayer> players(MAX_PLA YERS);
        for each player
        setup(players[i]);

        The latter would probably be quite ugly.


        Comment

        • Nick Hounsome

          #5
          Re: std::vector - bug or feature?


          "Janina Kramer" <j.kramer@schoo l-sucks.com> wrote in message
          news:c5bjos$1su $04$1@news.t-online.com...[color=blue]
          > hi ng,
          >
          > i'm working on a multiplayer game for a variable number of players and on[/color]
          the client side, i'm using a std::vector<CPl ayer> to store[color=blue]
          > informatik about the players. CPlayer is a class that contains another[/color]
          std::vector<CPo sition>. Because one of the players is the[color=blue]
          > client itself (and the size of the vector<CPlayer> doesn't change during a[/color]
          game), i thought i could store a[color=blue]
          > std::vector<CPl ayer>::iterator "localplaye r" that points to the respective[/color]
          element of the vector.[color=blue]
          > The strange thing is that when i add elements to the[/color]
          std::vector<CPo sition> (which is nothing more than a usual member of
          CPlayer)[color=blue]
          > the iterator "localplaye r" becomes somehow invalid (in a way that the[/color]
          memory it points to is no longer the actual "localplaye r" but[color=blue]
          > rather some random position in memory). what's wrong here? i didn't change[/color]
          anything about the std::vector<CPl ayer> so why can't i[color=blue]
          > use the "localplaye r" iterator any more? and: would it help to store a[/color]
          reference to the element rather than an iterator? i fixed[color=blue]
          > this problem by using an index instead of iterator, but i'm really curious[/color]
          about this.[color=blue]
          >
          > thanks in advance
          > janina[/color]

          I'm afraid that you must have screwed up somewhere because what you say
          sounds OK and
          I'm willing to bet a lot of money that there is no bug like that which you
          describe in std::vector.

          Are you sure that the size of the CPlayer vector doesn't change?
          How do you create it and how do you add players?
          The only reasonables way to do what you say are:

          std::vector<CPl ayer> players;
          players.reserve (MAX_PLAYERS);
          for up to MAX_PLAYERS
          players.push_ba ck(player);

          or

          std::vector<CPl ayer> players(MAX_PLA YERS);
          for each player
          setup(players[i]);

          The latter would probably be quite ugly.


          Comment

          • Buster

            #6
            Re: std::vector - bug or feature?

            Janina Kramer wrote:
            [color=blue]
            > i'm working on a multiplayer game for a variable number of players and on the client side, i'm using a std::vector<CPl ayer> to store
            > informatik about the players. CPlayer is a class that contains another std::vector<CPo sition>. Because one of the players is the
            > client itself (and the size of the vector<CPlayer> doesn't change during a game), i thought i could store a
            > std::vector<CPl ayer>::iterator "localplaye r" that points to the respective element of the vector.
            > The strange thing is that when i add elements to the std::vector<CPo sition> (which is nothing more than a usual member of CPlayer)
            > the iterator "localplaye r" becomes somehow invalid (in a way that the memory it points to is no longer the actual "localplaye r" but
            > rather some random position in memory). what's wrong here? i didn't change anything about the std::vector<CPl ayer> so why can't i
            > use the "localplaye r" iterator any more? and: would it help to store a reference to the element rather than an iterator? i fixed
            > this problem by using an index instead of iterator, but i'm really curious about this.[/color]

            If we're guessing, I'll take a shot:

            Have you passed a vector into a function by value?

            In C++, function arguments are by default passed 'by value':
            the argument is copied into the parameter. If the argument is
            a vector, this means its elements are copied one by one.
            This is very different from Java, which uses reference semantics.
            In C++ if you want reference semantics you have to say so.
            Here's an example.

            #include <vector>

            #ifdef WRONG

            // By value: v will be a brand new copy of the argument.
            std::vector <int>::const_it erator f (std::vector <int> v)
            {
            return std::find (v.begin (), v.end (), 7);
            // The vector 'v' is destroyed here, so the returned
            // iterator is invalid.
            }

            #else

            // By reference: v will be a reference to the argument.
            std::vector <int>::const_it erator f (std::vector <int> & v)
            {
            return std::find (v.begin (), v.end (), 7);
            // Only the reference 'v' is destroyed. The
            // vector it refers to is not affected, so
            // the iterator remains valid.
            }

            #endif

            int main ()
            {
            std::vector <int> u;
            u.push_back (7);

            if (f (u) == u.end ()) return 1;
            else return 0;
            }


            --
            Regards,
            Buster.

            Comment

            • Buster

              #7
              Re: std::vector - bug or feature?

              Janina Kramer wrote:
              [color=blue]
              > i'm working on a multiplayer game for a variable number of players and on the client side, i'm using a std::vector<CPl ayer> to store
              > informatik about the players. CPlayer is a class that contains another std::vector<CPo sition>. Because one of the players is the
              > client itself (and the size of the vector<CPlayer> doesn't change during a game), i thought i could store a
              > std::vector<CPl ayer>::iterator "localplaye r" that points to the respective element of the vector.
              > The strange thing is that when i add elements to the std::vector<CPo sition> (which is nothing more than a usual member of CPlayer)
              > the iterator "localplaye r" becomes somehow invalid (in a way that the memory it points to is no longer the actual "localplaye r" but
              > rather some random position in memory). what's wrong here? i didn't change anything about the std::vector<CPl ayer> so why can't i
              > use the "localplaye r" iterator any more? and: would it help to store a reference to the element rather than an iterator? i fixed
              > this problem by using an index instead of iterator, but i'm really curious about this.[/color]

              If we're guessing, I'll take a shot:

              Have you passed a vector into a function by value?

              In C++, function arguments are by default passed 'by value':
              the argument is copied into the parameter. If the argument is
              a vector, this means its elements are copied one by one.
              This is very different from Java, which uses reference semantics.
              In C++ if you want reference semantics you have to say so.
              Here's an example.

              #include <vector>

              #ifdef WRONG

              // By value: v will be a brand new copy of the argument.
              std::vector <int>::const_it erator f (std::vector <int> v)
              {
              return std::find (v.begin (), v.end (), 7);
              // The vector 'v' is destroyed here, so the returned
              // iterator is invalid.
              }

              #else

              // By reference: v will be a reference to the argument.
              std::vector <int>::const_it erator f (std::vector <int> & v)
              {
              return std::find (v.begin (), v.end (), 7);
              // Only the reference 'v' is destroyed. The
              // vector it refers to is not affected, so
              // the iterator remains valid.
              }

              #endif

              int main ()
              {
              std::vector <int> u;
              u.push_back (7);

              if (f (u) == u.end ()) return 1;
              else return 0;
              }


              --
              Regards,
              Buster.

              Comment

              • Janina Kramer

                #8
                Re: std::vector - bug or feature?

                i'm using iterators only. no index-access at all.

                "Leor Zolman" <leor@bdsoft.co m> schrieb im Newsbeitrag news:mrni701vu2 havdf81n43h20ga 8d0n54sja@4ax.c om...[color=blue]
                > On Sun, 11 Apr 2004 16:12:40 +0200, "Janina Kramer"
                > <j.kramer@schoo l-sucks.com> wrote:
                >[color=green]
                > >hi ng,
                > >
                > >i'm working on a multiplayer game for a variable number of players and on the client side, i'm using a std::vector<CPl ayer> to[/color][/color]
                store[color=blue][color=green]
                > >informatik about the players. CPlayer is a class that contains another std::vector<CPo sition>. Because one of the players is the
                > >client itself (and the size of the vector<CPlayer> doesn't change during a game), i thought i could store a
                > >std::vector<CP layer>::iterato r "localplaye r" that points to the respective element of the vector.
                > >The strange thing is that when i add elements to the std::vector<CPo sition> (which is nothing more than a usual member of[/color][/color]
                CPlayer)[color=blue][color=green]
                > >the iterator "localplaye r" becomes somehow invalid (in a way that the memory it points to is no longer the actual "localplaye r"[/color][/color]
                but[color=blue][color=green]
                > >rather some random position in memory). what's wrong here? i didn't change anything about the std::vector<CPl ayer> so why can't i
                > >use the "localplaye r" iterator any more? and: would it help to store a reference to the element rather than an iterator? i fixed
                > >this problem by using an index instead of iterator, but i'm really curious about this.[/color]
                >
                > Offhand, what you've described doesn't seem like it should be invalidating
                > that iterator all by itself. Perhaps the iterator is just getting corrupted
                > by some bug in your program (e.g., assignment using an out-of-bounds vector
                > index)?
                > -leor
                >[color=green]
                > >
                > >thanks in advance
                > >janina
                > >[/color]
                >
                > --
                > Leor Zolman --- BD Software --- www.bdsoft.com
                > On-Site Training in C/C++, Java, Perl and Unix
                > C++ users: Download BD Software's free STL Error Message Decryptor at:
                > www.bdsoft.com/tools/stlfilt.html[/color]


                Comment

                • Janina Kramer

                  #9
                  Re: std::vector - bug or feature?

                  the vector<CPlayer> is a member of the CGame class and it is never passed to any function.

                  "Buster" <noone@nowhere. com> schrieb im Newsbeitrag news:c5brg7$6hp $1@news6.svr.po l.co.uk...[color=blue]
                  > Janina Kramer wrote:
                  >[color=green]
                  > > i'm working on a multiplayer game for a variable number of players and on the client side, i'm using a std::vector<CPl ayer> to[/color][/color]
                  store[color=blue][color=green]
                  > > informatik about the players. CPlayer is a class that contains another std::vector<CPo sition>. Because one of the players is the
                  > > client itself (and the size of the vector<CPlayer> doesn't change during a game), i thought i could store a
                  > > std::vector<CPl ayer>::iterator "localplaye r" that points to the respective element of the vector.
                  > > The strange thing is that when i add elements to the std::vector<CPo sition> (which is nothing more than a usual member of[/color][/color]
                  CPlayer)[color=blue][color=green]
                  > > the iterator "localplaye r" becomes somehow invalid (in a way that the memory it points to is no longer the actual "localplaye r"[/color][/color]
                  but[color=blue][color=green]
                  > > rather some random position in memory). what's wrong here? i didn't change anything about the std::vector<CPl ayer> so why can't[/color][/color]
                  i[color=blue][color=green]
                  > > use the "localplaye r" iterator any more? and: would it help to store a reference to the element rather than an iterator? i fixed
                  > > this problem by using an index instead of iterator, but i'm really curious about this.[/color]
                  >
                  > If we're guessing, I'll take a shot:
                  >
                  > Have you passed a vector into a function by value?
                  >
                  > In C++, function arguments are by default passed 'by value':
                  > the argument is copied into the parameter. If the argument is
                  > a vector, this means its elements are copied one by one.
                  > This is very different from Java, which uses reference semantics.
                  > In C++ if you want reference semantics you have to say so.
                  > Here's an example.
                  >
                  > #include <vector>
                  >
                  > #ifdef WRONG
                  >
                  > // By value: v will be a brand new copy of the argument.
                  > std::vector <int>::const_it erator f (std::vector <int> v)
                  > {
                  > return std::find (v.begin (), v.end (), 7);
                  > // The vector 'v' is destroyed here, so the returned
                  > // iterator is invalid.
                  > }
                  >
                  > #else
                  >
                  > // By reference: v will be a reference to the argument.
                  > std::vector <int>::const_it erator f (std::vector <int> & v)
                  > {
                  > return std::find (v.begin (), v.end (), 7);
                  > // Only the reference 'v' is destroyed. The
                  > // vector it refers to is not affected, so
                  > // the iterator remains valid.
                  > }
                  >
                  > #endif
                  >
                  > int main ()
                  > {
                  > std::vector <int> u;
                  > u.push_back (7);
                  >
                  > if (f (u) == u.end ()) return 1;
                  > else return 0;
                  > }
                  >
                  >
                  > --
                  > Regards,
                  > Buster.[/color]


                  Comment

                  • Janina Kramer

                    #10
                    Re: std::vector - bug or feature?

                    i'm using the players.push_ba ck(player) method (without reserve(..) - does that matter?)
                    i am very sure that the size of the vector doesn't change after assigning the "localplaye r" iterator. the number of players is
                    constant for each round because the CPlayers have isConnected() and such kind of methods, so no need to change the vector size. and
                    the "localplaye r" iterator is assigned after the init-round stuff.

                    "Nick Hounsome" <nh002@blueyond er.co.uk> schrieb im Newsbeitrag news:5tdec.6221 5$Id.40555@news-binary.blueyond er.co.uk...[color=blue]
                    >
                    > "Janina Kramer" <j.kramer@schoo l-sucks.com> wrote in message
                    > news:c5bjos$1su $04$1@news.t-online.com...[color=green]
                    > > hi ng,
                    > >
                    > > i'm working on a multiplayer game for a variable number of players and on[/color]
                    > the client side, i'm using a std::vector<CPl ayer> to store[color=green]
                    > > informatik about the players. CPlayer is a class that contains another[/color]
                    > std::vector<CPo sition>. Because one of the players is the[color=green]
                    > > client itself (and the size of the vector<CPlayer> doesn't change during a[/color]
                    > game), i thought i could store a[color=green]
                    > > std::vector<CPl ayer>::iterator "localplaye r" that points to the respective[/color]
                    > element of the vector.[color=green]
                    > > The strange thing is that when i add elements to the[/color]
                    > std::vector<CPo sition> (which is nothing more than a usual member of
                    > CPlayer)[color=green]
                    > > the iterator "localplaye r" becomes somehow invalid (in a way that the[/color]
                    > memory it points to is no longer the actual "localplaye r" but[color=green]
                    > > rather some random position in memory). what's wrong here? i didn't change[/color]
                    > anything about the std::vector<CPl ayer> so why can't i[color=green]
                    > > use the "localplaye r" iterator any more? and: would it help to store a[/color]
                    > reference to the element rather than an iterator? i fixed[color=green]
                    > > this problem by using an index instead of iterator, but i'm really curious[/color]
                    > about this.[color=green]
                    > >
                    > > thanks in advance
                    > > janina[/color]
                    >
                    > I'm afraid that you must have screwed up somewhere because what you say
                    > sounds OK and
                    > I'm willing to bet a lot of money that there is no bug like that which you
                    > describe in std::vector.
                    >
                    > Are you sure that the size of the CPlayer vector doesn't change?
                    > How do you create it and how do you add players?
                    > The only reasonables way to do what you say are:
                    >
                    > std::vector<CPl ayer> players;
                    > players.reserve (MAX_PLAYERS);
                    > for up to MAX_PLAYERS
                    > players.push_ba ck(player);
                    >
                    > or
                    >
                    > std::vector<CPl ayer> players(MAX_PLA YERS);
                    > for each player
                    > setup(players[i]);
                    >
                    > The latter would probably be quite ugly.
                    >
                    >[/color]


                    Comment

                    • Janina Kramer

                      #11
                      further explanations

                      sorry, forgot to mention that i'm not a newbie. actually, i don't really think that this problem is a bug in the c++-libs (because
                      someone would have certainly noticed that before i did), but on the other hand i know for sure that i don't change the
                      vector<CPlayer> in any way after assigning the "localplaye r" iterator. i debugged the project line by line and can say that the
                      exact place where the localplayer iterator becomes invalid is after a call to position.push_b ack(..) (where position is the already
                      mentioned CPlayer member of type vector<CPositio n>).

                      thanks again
                      janina

                      "Janina Kramer" <j.kramer@schoo l-sucks.com> schrieb im Newsbeitrag news:c5bjos$1su $04$1@news.t-online.com...[color=blue]
                      > hi ng,
                      >
                      > i'm working on a multiplayer game for a variable number of players and on the client side, i'm using a std::vector<CPl ayer> to[/color]
                      store[color=blue]
                      > informatik about the players. CPlayer is a class that contains another std::vector<CPo sition>. Because one of the players is the
                      > client itself (and the size of the vector<CPlayer> doesn't change during a game), i thought i could store a
                      > std::vector<CPl ayer>::iterator "localplaye r" that points to the respective element of the vector.
                      > The strange thing is that when i add elements to the std::vector<CPo sition> (which is nothing more than a usual member of CPlayer)
                      > the iterator "localplaye r" becomes somehow invalid (in a way that the memory it points to is no longer the actual "localplaye r"[/color]
                      but[color=blue]
                      > rather some random position in memory). what's wrong here? i didn't change anything about the std::vector<CPl ayer> so why can't i
                      > use the "localplaye r" iterator any more? and: would it help to store a reference to the element rather than an iterator? i fixed
                      > this problem by using an index instead of iterator, but i'm really curious about this.
                      >
                      > thanks in advance
                      > janina
                      >
                      >[/color]


                      Comment

                      • Leor Zolman

                        #12
                        Re: further explanations

                        On Mon, 12 Apr 2004 03:02:45 +0200, "Janina Kramer"
                        <j.kramer@schoo l-sucks.com> wrote:
                        [color=blue]
                        >sorry, forgot to mention that i'm not a newbie. actually, i don't really think that this problem is a bug in the c++-libs (because
                        >someone would have certainly noticed that before i did), but on the other hand i know for sure that i don't change the
                        >vector<CPlayer > in any way after assigning the "localplaye r" iterator. i debugged the project line by line and can say that the
                        >exact place where the localplayer iterator becomes invalid is after a call to position.push_b ack(..) (where position is the already
                        >mentioned CPlayer member of type vector<CPositio n>).
                        >
                        >thanks again
                        >janina
                        >[/color]

                        Since folks seem stumped, perhaps you can give us more informatik? :-)
                        For starters, the compiler, version and lib you're using, the declarations
                        of all the data types involved, and the code where you've seen things go
                        haywire. No promises, but the guesswork around here tends to get much more
                        accurate once folks see more of those kinds of little details.
                        -leor

                        --
                        Leor Zolman --- BD Software --- www.bdsoft.com
                        On-Site Training in C/C++, Java, Perl and Unix
                        C++ users: Download BD Software's free STL Error Message Decryptor at:
                        An STL Error Decryptor for C++ by Leor Zolman of BD Software - available to download here

                        Comment

                        • Ian

                          #13
                          Re: further explanations

                          Janina Kramer wrote:[color=blue]
                          > sorry, forgot to mention that i'm not a newbie. actually, i don't really think that this problem is a bug in the c++-libs (because
                          > someone would have certainly noticed that before i did), but on the other hand i know for sure that i don't change the
                          > vector<CPlayer> in any way after assigning the "localplaye r" iterator. i debugged the project line by line and can say that the
                          > exact place where the localplayer iterator becomes invalid is after a call to position.push_b ack(..) (where position is the already
                          > mentioned CPlayer member of type vector<CPositio n>).
                          >[/color]
                          Please don't top post!

                          Do you have access to an access checking tool or debugger? If so, try
                          it and see what happens.

                          If not, post some code!

                          Ian
                          [color=blue]
                          >
                          > "Janina Kramer" <j.kramer@schoo l-sucks.com> schrieb im Newsbeitrag news:c5bjos$1su $04$1@news.t-online.com...
                          >[color=green]
                          >>hi ng,
                          >>
                          >>i'm working on a multiplayer game for a variable number of players and on the client side, i'm using a std::vector<CPl ayer> to[/color]
                          >
                          > store
                          >[color=green]
                          >>informatik about the players. CPlayer is a class that contains another std::vector<CPo sition>. Because one of the players is the
                          >>client itself (and the size of the vector<CPlayer> doesn't change during a game), i thought i could store a
                          >>std::vector<C Player>::iterat or "localplaye r" that points to the respective element of the vector.
                          >>The strange thing is that when i add elements to the std::vector<CPo sition> (which is nothing more than a usual member of CPlayer)
                          >>the iterator "localplaye r" becomes somehow invalid (in a way that the memory it points to is no longer the actual "localplaye r"[/color]
                          >
                          > but
                          >[color=green]
                          >>rather some random position in memory). what's wrong here? i didn't change anything about the std::vector<CPl ayer> so why can't i
                          >>use the "localplaye r" iterator any more? and: would it help to store a reference to the element rather than an iterator? i fixed
                          >>this problem by using an index instead of iterator, but i'm really curious about this.
                          >>
                          >>thanks in advance
                          >>janina
                          >>
                          >>[/color]
                          >
                          >
                          >[/color]

                          Comment

                          • Janina Kramer

                            #14
                            Re: further explanations


                            "Ian" <noone@nowhere. com> schrieb im Newsbeitrag news:1081744606 .262055@drone5. qsi.net.nz...[color=blue]
                            > Janina Kramer wrote:[color=green]
                            > > sorry, forgot to mention that i'm not a newbie. actually, i don't really think that this problem is a bug in the c++-libs[/color][/color]
                            (because[color=blue][color=green]
                            > > someone would have certainly noticed that before i did), but on the other hand i know for sure that i don't change the
                            > > vector<CPlayer> in any way after assigning the "localplaye r" iterator. i debugged the project line by line and can say that the
                            > > exact place where the localplayer iterator becomes invalid is after a call to position.push_b ack(..) (where position is the[/color][/color]
                            already[color=blue][color=green]
                            > > mentioned CPlayer member of type vector<CPositio n>).
                            > >[/color]
                            > Please don't top post!
                            >
                            > Do you have access to an access checking tool or debugger? If so, try
                            > it and see what happens.
                            >[/color]

                            i used the gdb was just what i wanted to say with "i debugged the project line by line".

                            janina
                            [color=blue]
                            > If not, post some code!
                            >
                            > Ian
                            >[/color]


                            Comment

                            • Buster

                              #15
                              Re: further explanations

                              Stop trolling. If you still want help, post some code.

                              --
                              Regards,
                              Buster.

                              Comment

                              Working...