473,545 Members | 2,055 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 2847
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
5912
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...
20
17752
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
3334
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...
8
5095
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...
32
69644
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
5711
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
8886
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
2943
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
7475
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...
0
7664
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7918
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...
1
7436
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7766
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...
0
5981
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5341
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...
0
4958
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...
1
1897
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.