473,708 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Jul 22 '05 #1
18 2867
On Sun, 11 Apr 2004 16:12:40 +0200, "Janina Kramer"
<j.******@schoo l-sucks.com> wrote:
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.
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

thanks in advance
janina


--
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
Jul 22 '05 #2
On Sun, 11 Apr 2004 16:12:40 +0200, "Janina Kramer"
<j.******@schoo l-sucks.com> wrote:
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.
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

thanks in advance
janina


--
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
Jul 22 '05 #3

"Janina Kramer" <j.******@schoo l-sucks.com> wrote in message
news:c5******** *****@news.t-online.com...
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


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.
Jul 22 '05 #4

"Janina Kramer" <j.******@schoo l-sucks.com> wrote in message
news:c5******** *****@news.t-online.com...
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


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.
Jul 22 '05 #5
Janina Kramer wrote:
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.


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.
Jul 22 '05 #6
Janina Kramer wrote:
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.


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.
Jul 22 '05 #7
i'm using iterators only. no index-access at all.

"Leor Zolman" <le**@bdsoft.co m> schrieb im Newsbeitrag news:mr******** *************** *********@4ax.c om...
On Sun, 11 Apr 2004 16:12:40 +0200, "Janina Kramer"
<j.******@schoo l-sucks.com> wrote:
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 storeinformatik 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" butrather 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.


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

thanks in advance
janina


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

Jul 22 '05 #8
the vector<CPlayer> is a member of the CGame class and it is never passed to any function.

"Buster" <no***@nowhere. com> schrieb im Newsbeitrag news:c5******** **@news6.svr.po l.co.uk...
Janina Kramer wrote:
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.


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.

Jul 22 '05 #9
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" <nh***@blueyond er.co.uk> schrieb im Newsbeitrag news:5t******** **********@news-binary.blueyond er.co.uk...

"Janina Kramer" <j.******@schoo l-sucks.com> wrote in message
news:c5******** *****@news.t-online.com...
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


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.

Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
5959
by: Jason Heyes | last post by:
To my understanding, std::vector does not use reference counting to avoid the overhead of copying and initialisation. Where can I get a reference counted implementation of std::vector? Thanks.
20
443
by: Janina Kramer | last post by:
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<CPlayer> to store informatik about the players. CPlayer is a class that contains another std::vector<CPosition>. 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<CPlayer>::iterator "localplayer" that points to the...
20
17823
by: Anonymous | last post by:
Is there a non-brute force method of doing this? transform() looked likely but had no predefined function object. std::vector<double> src; std::vector<int> dest; std::vector<double>::size_type size = src.size(); dest.reserve(size); for (std::vector<int>::size_type i = 0;
17
3351
by: Michael Hopkins | last post by:
Hi all I want to create a std::vector that goes from 1 to n instead of 0 to n-1. The only change this will have is in loops and when the vector returns positions of elements etc. I am calling this uovec at the moment (for Unit-Offset VECtor). I want the class to respond correctly to all usage of STL containers and algorithms so that it is a transparent replacement for std:vector. The options seems to be:
8
5109
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value types or std::vector<int>. So where I would use an int* and reallocate it from time to time in C, and randomly access it via , then I figure to copy the capacity and reserve methods, because I just need a growable array. I get to considering...
32
69682
by: zl2k | last post by:
hi, c++ user Suppose I constructed a large array and put it in the std::vector in a function and now I want to return it back to where the function is called. I can do like this: std::vector<int> fun(){ //build the vector v; return v; }
56
5789
by: Peter Olcott | last post by:
I am trying to refer to the same std::vector in a class by two different names, I tried a union, and I tried a reference, I can't seem to get the syntax right. Can anyone please help? Thanks
9
8896
by: aaragon | last post by:
I am trying to create a vector of type T and everything goes fine until I try to iterate over it. For some reason, the compiler gives me an error when I declare std::vector<T>::iterator iter; Any ideas why is tihs happening? The code is as follows: template <class T> struct StdVectorStorage { std::vector<T>* _storage;
13
2956
by: jubelbrus | last post by:
Hi I'm trying to do the following. #include <vector> #include <boost/thread/mutex.hpp> #include <boost/shared_ptr.hpp> #include <boost/tuple/tuple.hpp> class {
0
8788
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8697
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9159
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9001
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6615
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5939
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4713
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2508
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2097
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.