Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

BSMonkey

Playing with vectors

This topic is 5154 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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[i - 1]->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[i]; 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 one tell me why please.

Share this post


Link to post
Share on other sites
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() );

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!