By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,530 Members | 880 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,530 IT Pros & Developers. It's quick & easy.

C++ Vector of objects

P: 1
Hello,

I'm just starting to learn C++, coming from a Java / bit of C background. I have some questions about how vectors of objects and pointers work in C++.

Say I want to create a vector of SomeClass objects, and the SomeClass constructor takes one argument. However, SomeClass is not copyable, so I can't do something like:
Expand|Select|Wrap|Line Numbers
  1. vector<SomeClass> someVector;
  2. for (int i=0; i<args.size(); i++){
  3.     someVector.push_back( SomeClass(args[i]) )
  4. }
  5.  
So instead, I used a temporary variable and stored pointers inside the vector instead:
Expand|Select|Wrap|Line Numbers
  1. vector<SomeClass *> someVector;
  2. for (int i=0; i<args.size(); i++){
  3.     SomeClass temp(args[i]);
  4.     someVector.push_back(&temp);
  5. }
  6.  
However, all the pointers in someVector ended up pointing to temp(args[args.size()-1]). I've resolved this by using boost::shared_ptr instead, but I wanted to understand why the above code didn't work as expected. I thought that in each iteration of the for loop, a new temp will get created, and a reference to it will be stored in someVector (so that it won't garbage collected or w/e since there's still a reference to it). The same thing also seems to happen when I make a vector of a struct...

From general Googling, I get the feeling that in general, it's probably a good idea to get into the habit of storing pointers instead of actual objects and structs in vectors - so I should probably get used to using vectors of shared_ptr?

Also, any other general comments, tips, common gotchas, etc. will be greatly appreciated (trying to learn all I can!).

Thanks!
Aug 11 '07 #1
Share this Question
Share on Google+
4 Replies


Expert 10K+
P: 11,448
Expand|Select|Wrap|Line Numbers
  1. vector<SomeClass *> someVector;
  2. for (int i=0; i<args.size(); i++){
  3.     SomeClass temp(args[i]);
  4.     someVector.push_back(&temp);
  5. }
  6.  
However, all the pointers in someVector ended up pointing to temp(args[args.size()-1]).
Yep, 'temp' is just a local variable and no matter how many times your evaluate
'&temp' it'll always be the address of that same local variable. Do this instead:

Expand|Select|Wrap|Line Numbers
  1. vector<SomeClass *> someVector;
  2. for (int i=0; i<args.size(); i++){
  3.     SomeClass* temp= new SomeClass(args[i]);
  4.     someVector.push_back(temp);
  5. }
  6.  
See the difference?

kind regards,

Jos
Aug 11 '07 #2

P: 86
Yep, 'temp' is just a local variable and no matter how many times your evaluate
'&temp' it'll always be the address of that same local variable. Do this instead:

Expand|Select|Wrap|Line Numbers
  1. vector<SomeClass *> someVector;
  2. for (int i=0; i<args.size(); i++){
  3.     SomeClass* temp= new SomeClass(args[i]);
  4.     someVector.push_back(temp);
  5. }
  6.  
See the difference?

kind regards,

Jos
and since now you are using new, you can drop the temp and go back to your original design someVector.push_back(new SomeClass(args[i]))
Aug 11 '07 #3

P: 1
Hi. Respect to the example. Could you please tell me the right way to delete all the memory allocated from this piece of code.

i believe that i have to do something like this.

vector<SomeClass *> someVector;

for (int i=0; i<args.size(); i++){
SomeClass* temp= new SomeClass(args[i]);
someVector.push_back(temp);

delete temp; // Is necessary??
}

//and next;

int VecSize = someVector.size();
for (int i=0; i<VecSize; i++)
{
delete someVector[i]; //Is correct?
}

thanks for your help
Feb 28 '12 #4

P: 1
don't delete temp within assignment for loop

//do all the work you wanted to do with someVector
// then delete like you showed in the last forloop, before you go out of scope
Mar 5 '13 #5

Post your reply

Sign in to post your reply or Sign up for a free account.