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.
"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