Click here to Skip to main content
15,918,168 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
typedef vector< boost::shared_ptr< CElement > > SharedList;
int size = 12;
SharedList list = SharedList( size );


Will the vector be destroyed when it goes out of scope? Are there times when I'd be required to call clear()?

Would I still have to call reserve() in this case if I don't want reallocations?

Does [the above code] just fill the vector with null [shared] pointers? Seems like a bad practice, am I wrong?

1st Edit:

Thanks, Superman.

I do understand how vectors work with raw pointers. So the real question is; When using smart pointers, where the pointers should automatically delete themselves, are there special circumstances with vectors where they might not get deleted? Can I rely on "vector going out of scope" to delete the smart pointers?

Is the only possible leak a cyclic reference?

And also the question about reserve wasn't answered. Is it true that I wouldn't need to use reserve() in the code example above and still avoid re-allocations?

2nd Edit:

Thanks, peterchen. Really learned a lot there.
Posted
Updated 16-Feb-10 22:31pm
v10

A vector will be destroyed when it goes out of scope, meaning the memory allocated for the vector will be destroyed.
But if its contents need to be destroyed independently, it has to be done manually before the vector goes out of scope.

For example, if you have a vector of pointers and these pointers are allocated memory dynamically, only the space allocated for storing the pointers will be destroyed automatically when the vector goes out of scope. The dynamically allocated pointers therefore must be destroyed by iterating through the vector before it goes out of scope.

reserve() will initially allocate some minimum space to the vector. Reallocations will still occur whenever more space is needed.

Yes, after calling reserve(), the allocated space will initially contain zeros or NULLs depending on the content type.
 
Share this answer
 
1. Yes, the vector will be destroyed. CElement's will be destroyed if the vector held the last reference to them.

2. Circular reference is the only possibility for a leak (unless you manipulate the implementation details of the pointer, e.g. artificially manually unsupportedly icnreasing the reference count)

3. You might access an already-destroyed element when you violate the once-shared-always-shared rule:

C++
boost__shared_ptr<a> aptr(new a);
a * pa = aptr.get();
boost::shared_ptr<a> aptr2(pa);  // ERROR: creating separate shared_ptr to same object
aptr2.reset(); // releaseing one, FATAL: aptr is stale!


Note that this is OK with intrusive_ptr.

4. Yes, the same rules for vector apply: if you call resize (or the sizing constructor), default constructed elements will be appended. reserve with reserve the memorys without constructing the elements.

Note that without reserve, the reallocations still happen - but at least the copies are cheap.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900