• Advertisement

Archived

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

Playing with vectors

This topic is 4976 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
quote:

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,
And what reason would that be? I don''t think the compiler would do a random stuff like that. One time it would destroy objects and other times it would not.

I think this is the same scenario as C-string.

char* pchar = "Hello";
if ( pchar == "Hello" )
{
}

There is only one instance of "Hello" exists in the memory, thus the if statement above would yield true; a common misconception for newbies when comparing C-strings.

When you type in dog(), the compiler will create only one object in the stack for the whole execution of the program. Since OP calls this dog() multiple times in a loop, the constructor gets called multiple times and since this is the same object, it''s the same dog::val that gets incremented.

Share this post


Link to post
Share on other sites
quote:
Original post by alnite
quote:

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,
And what reason would that be? I don''t think the compiler would do a random stuff like that. One time it would destroy objects and other times it would not.

I think this is the same scenario as C-string.

char* pchar = "Hello";
if ( pchar == "Hello" )
{
}

There is only one instance of "Hello" exists in the memory, thus the if statement above would yield true; a common misconception for newbies when comparing C-strings.

When you type in dog(), the compiler will create only one object in the stack for the whole execution of the program. Since OP calls this dog() multiple times in a loop, the constructor gets called multiple times and since this is the same object, it''s the same dog::val that gets incremented.


I done abit of testing and your wright there, didn''t seem logical to me. I Just don''t see whacky code like that normally so i wouldn''t have known for definate.

Share this post


Link to post
Share on other sites
I was just playing around trying to find a bug in another program. I found it was was doing back( ) on a queue then pop( ) I shoulda been doing front ( ) then pop( )

Share this post


Link to post
Share on other sites

  • Advertisement