std::vector - bug or feature?

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

    #16
    Re: further explanations

    "Janina Kramer" <j.kramer@schoo l-sucks.com> wrote:

    [quoted text edited slightly for conciseness][color=blue]
    >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> the iterator
    >"localplayer " 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?[/color]
    [color=blue]
    >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(..)[/color]

    std::vector is required to store all of its items in a contiguous block
    of memory. If you add a new item, and the vector is already 'full'
    (ie. it is using all of the memory it has allocated so far), it has to
    allocate new memory. Usually this involves allocating an entire new
    large block of memory, copying all the values over, and releasing
    the old memory. Obviously, this is why your iterators are now pointing
    to the middle of nowhere.

    You could either use a std::list (which allows insertion and deletion
    without invalidating iterators/pointers/references), or keep using
    a std::vector but call the "reserve()" member function beforehand.
    This function pre-allocates memory for as many elements as you would
    like, so that as long as you make sure the actual number of elements
    doesn't exceed this limit, you can safely insert and delete without
    invalidating your iterators.

    Comment

    • Leor Zolman

      #17
      Re: further explanations

      On 12 Apr 2004 14:24:34 -0700, oldwolf@inspire .net.nz (Old Wolf) wrote:
      [color=blue]
      >std::vector is required to store all of its items in a contiguous block
      >of memory. If you add a new item, and the vector is already 'full'
      >(ie. it is using all of the memory it has allocated so far), it has to
      >allocate new memory. Usually this involves allocating an entire new
      >large block of memory, copying all the values over, and releasing
      >the old memory. Obviously, this is why your iterators are now pointing
      >to the middle of nowhere.
      >[/color]
      I think in this case we've been thinking about what would happen if there
      were an iterator /to the vector/ that has just grown. Different issue. But
      given that the OP still hasn't seen fit to post any /code/ to help us help
      him with his problem, I'm not surprised you read it that way...
      -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

      • Janina Kramer

        #18
        code


        "Leor Zolman" <leor@bdsoft.co m> schrieb im Newsbeitrag news:ij2m70t698 8vt8r3i4roloe2h gp09objfs@4ax.c om...[color=blue]
        > On 12 Apr 2004 14:24:34 -0700, oldwolf@inspire .net.nz (Old Wolf) wrote:
        >[/color]
        [color=blue]
        > I think in this case we've been thinking about what would happen if there
        > were an iterator /to the vector/ that has just grown. Different issue. But
        > given that the OP still hasn't seen fit to post any /code/ to help us help
        > him with his problem, I'm not surprised you read it that way...
        > -leor[/color]

        excuse me, but i really don't know what code i shall post here. the one that doesn't change the players-vector or the one that makes
        the call to position.push_b ack(..) without influencing the players-vector in any way. both wouldn't make much sense, would it?
        (might sound like a joke, but it's not)

        janina
        [color=blue]
        >
        > --
        > 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

        • Leor Zolman

          #19
          Re: code

          On Tue, 13 Apr 2004 00:33:52 +0200, "Janina Kramer"
          <j.kramer@schoo l-sucks.com> wrote:[color=blue]
          >
          >excuse me, but i really don't know what code i shall post here. the one that doesn't change the players-vector or the one that makes
          >the call to position.push_b ack(..) without influencing the players-vector in any way. both wouldn't make much sense, would it?
          >(might sound like a joke, but it's not)[/color]

          I don't know what code you shall post either, but I suspect I know how much
          additional help you shall receive if you post none at all (and that may in
          fact be at least a /little/ bit a joke.)
          -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

          • Janina Kramer

            #20
            Re: code


            "Leor Zolman" <leor@bdsoft.co m> schrieb im Newsbeitrag news:0c8m70ha9g n6goscncgneniat m38ali29p@4ax.c om...[color=blue]
            > On Tue, 13 Apr 2004 00:33:52 +0200, "Janina Kramer"
            > <j.kramer@schoo l-sucks.com> wrote:[color=green]
            > >
            > >excuse me, but i really don't know what code i shall post here. the one that doesn't change the players-vector or the one that[/color][/color]
            makes[color=blue][color=green]
            > >the call to position.push_b ack(..) without influencing the players-vector in any way. both wouldn't make much sense, would it?
            > >(might sound like a joke, but it's not)[/color]
            >
            > I don't know what code you shall post either, but I suspect I know how much
            > additional help you shall receive if you post none at all (and that may in
            > fact be at least a /little/ bit a joke.)
            > -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:
            > www.bdsoft.com/tools/stlfilt.html[/color]

            i'd just say you can't solve this riddle (you = this_ng) and no code in the world will ever change anything about that...

            not joking
            janina


            Comment

            • Leor Zolman

              #21
              Re: code

              On Tue, 13 Apr 2004 19:17:10 +0200, "Janina Kramer"
              <j.kramer@schoo l-sucks.com> wrote:
              [color=blue]
              >
              >"Leor Zolman" <leor@bdsoft.co m> schrieb im Newsbeitrag news:0c8m70ha9g n6goscncgneniat m38ali29p@4ax.c om...[color=green]
              >> On Tue, 13 Apr 2004 00:33:52 +0200, "Janina Kramer"
              >> <j.kramer@schoo l-sucks.com> wrote:[color=darkred]
              >> >
              >> >excuse me, but i really don't know what code i shall post here. the one that doesn't change the players-vector or the one that[/color][/color]
              >makes[color=green][color=darkred]
              >> >the call to position.push_b ack(..) without influencing the players-vector in any way. both wouldn't make much sense, would it?
              >> >(might sound like a joke, but it's not)[/color]
              >>
              >> I don't know what code you shall post either, but I suspect I know how much
              >> additional help you shall receive if you post none at all (and that may in
              >> fact be at least a /little/ bit a joke.)
              >> -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:
              >> www.bdsoft.com/tools/stlfilt.html[/color]
              >
              >i'd just say you can't solve this riddle (you = this_ng) and no code in the world will ever change anything about that...[/color]

              Do you actually believe that no one here would be able to shed any light on
              your problem if they were able to see some (or all) of the actual code
              involved? My experience has been that, collectively, this group (along with
              the others) prefers to reach resolution on questions. That resolution may
              come in the form of a bug fix, a clarification on behavior, perhaps a
              consensus that the behavior is unpredictable (usually accompanied with
              suggestions on how to make it predictable). But making any more progress
              toward such a solution in your case would seem to currently be hampered by
              a lack of information.

              If you think seeing the entire app would be necessary but that would make
              for too long of a post, you could ZIP up the app and post it on some web
              site for downloading. If you don't have a web site available to you, email
              me the file and I'll be more than happy to upload it on /my/ site, then
              post a link on this list. I'm really willing to help, and so is everyone
              else. But I'd still suggest starting with the aforementioned key elements
              of your app; it would probably take you less effort to just do that than
              you've already expended to try to convince us that it wouldn't do any good.

              So the first part of your concluding line above would seem to be correct,
              given the info we've been given so far. But I have serious doubts about
              that last part.
              -leor
              [color=blue]
              >
              >not joking
              >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

              Working...