Playing with vectors

Started by
11 comments, last by BSMonkey 19 years, 10 months ago
Ok Im working on an eneity manager and I have a lil but of lossing track of entities. I think its with how Im holding them in the vector. So I started playing around with vectors to see if they acted the way I expected. This is my lil test code. Sorry its so long I dont know how you people make nice lil scroll windows. #include <iostream> using std::cout; #include <math.h> #include <vector> using std::vector; //#include "Console.h" //#pragma comment(lib, "ConsoleD") /***********/ /* Globals */ /***********/ int count = 0; //Console con(80, 25, false, false); /**************/ /* Prototypes */ /**************/ class dog { public: const int val; dog( ) : val(++count) { } }; /************/ /* Internal */ /************/ int main(void) { vector < dog * > test; test.reserve( 5 ); for ( int i = 0; i < 10; ++i ) { test.push_back( &( dog( ) ) ); cout << test->val << "\n"; if( i ) cout << test->val << "\n"; } test[4] = 0; test[7] = 0; cout << "\n\n" << test.size( ) << "\n\n"; for ( int i = 0; i < 10; ++i ) { dog * temp = test; if ( temp ) cout << temp->val << ''\n''; } //con << Def; //con.Goto(con.Rows() - 1, (con.Cols() - 25) / 2); return 0; } this is the output it gives me but in a nice line 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 10 10 10 10 10 10 10 10 10 the end part of all tens is not what I expected at all. I thought it would count up from 1 to 10 skipping 5 and 8 but I get all tens. Can any &#111;ne tell me why please. </i>
Advertisement
quote:Original post by BSMonkey
test.push_back( &( dog( ) ) );

That line. You are passing the same object over and over again. Use test.push_back( new dog() );
I think you are filling the vector with pointers to the same address for which the last value it is changed to is 10. Try printing the addresses of the contents of the vector to check this.
-------------------------0 A.D.
ugg

thanks alnite I shoulda noticed that my self
Hi you can get the scrolly bars by putting your code between source /source tags.

Is there any particular reason to use a container of pointers in this case?, use erase method to delete elements, and size method for iteration or use iterators try this:
 #include <cmath>#include <vector>#include <iostream>class dog{public:int val;dog(const int v = 0) : val(v) {}};int main() {   std::vector < dog > test;   test.reserve(10);   for(int i = 0; i < 10; ++i ) {      test.push_back(dog(i));      std::cout << test[i].val << "\n";   }   test.erase(test.begin() + 4); //erase at index 4   test.erase(test.begin() + 7); //erase at index 7   std::cout << "\n\nsize: " << test.size() << "\n\n";   for(int i = 0; i < test.size(); ++i ) {      std::cout << test[i].val << '\n';   }   return 0;}


[edited by - snk_kid on June 9, 2004 3:42:39 PM]
oh, don''t forget to delete them.
Also, since you want to know how many dogs you created, make val static .

Edit: I see you've set the reserve to 5 but you're storing 10 objects. You might want to increase the reserve to 10 so the vector doesn't need to reallocate space.

[edited by - VStrider on June 9, 2004 4:00:39 PM]
___________________________________________________VRAM_Strider."This is the same damn ship that blows panels everytime it is shot, gets stolen on numerous occasions, has next to no security on any of its computer systems, allows almost anyone into the heart of the ship, and places the bridge and all the exec offices on the top of the ship?That enterprise?"
quote:Original post by alnite
quote:Original post by BSMonkey
test.push_back( &( dog( ) ) );

That line. You are passing the same object over and over again. Use test.push_back( new dog() );


Thats not exactly passing the same object, he''s calling dog''s constructor that temporally creates an instance on the stack prints it''s value and then is destroyed out of the scope of the loop automically. Then the addresses stored in the vector becomes invalid and points some arbitrary locations.
quote:Original post by snk_kid
Then the addresses stored in the vector becomes invalid and points some arbitrary locations.

Then why can he still access that destroyed object?
If it was the same instance then the first bunch of print outs would have also been the same value also but its not.

I guess that when the loop finishes it moves to the next statement and for some reason the last object on the stack hasn''t been destroyed by scope, its the only variable on the stack so i guess all the addressess stored in the vector have some base address (say the first half of the address) which is refering to the stack part of memory but its the only instance there.

This topic is closed to new replies.

Advertisement