Advertisement

A found a stl vector bug...

Started by June 30, 2003 02:28 PM
3 comments, last by cippyboy 21 years, 8 months ago
Here`s the bug`s code : #include <iostream> #include <vector> using namespace std; typedef struct DATA{ int *Val; float first; double second; DATA()//Data constructor { Val=new int;//We allocate it as a single int } }DATA; vector X;//Declare the stl vector void main() { DATA NewDATA; X.push_back(NewDATA);//Push Back 2 units X.push_back(NewDATA); X[0].Val[0]=5; X[1].Val[0]=10; int A=X[0].Val[0];//A should be 5 right ? int B=X[1].Val[0]; cout<<"A Value :"<<A<<endl;//Watch the vlue of them vars cout<<"B Value :"<<B<<endl; } So obviosly this works with a non-stl vector ... Have you ever stumbled upon something like this ? Should I report it anywhere else ? (hehe,like Microsoft)

Relative Games - My apps

That''s not a bug. If you read the documentation, you''ll see that STL containers make use of their datatype''s copy constructor, which you haven''t implemented. In other words, it''s not an STL vector bug, it''s a you bug.

Read up on "deep copy" vs. "shallow copy" for details.


How appropriate. You fight like a cow.
Advertisement
Hmm... vectors are templates, and you should declare you vector as vector var;. It could just be screwed up because it may be using memory addresses instead of data.

Legends Development Team
Legends Development Team
quote:

In other words, it''s not an STL vector bug, it''s a you bug.



Nice. I might have to use that from now on
Yeah, it''s you bug.

DATA NewDATA;
This allocates new int and you push this twice to X. Both has same pointer to Val.

This topic is closed to new replies.

Advertisement