arrays vs. vector

Started by
12 comments, last by BcS 20 years, 7 months ago
quasar3d, call vector::capacity instead of ::size.

size is how many elements are in the vector right now - capacity is how many will fit (without a reallocation).

- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Advertisement
If you have dynamic memory variables in a vector and call pop_back will it be the same as "delete variable" or could this lead to memory leaks?

--------------------
Though this program be madness, yet there is a method in''t
--------------------Though this program be madness, yet there is a method in't
a vector DOES NOT call delete ... period ... a vector manages the memory of the objects it contains ... it does NOT manage any memory POINTED to these obejcts, so if you have a vector of pointers to objects, the vector is NOT the owner of those objects (it will not automatically allocate and delete them) it is only the owner of the POINTERS to the objects ...

this is the ONLY way that makes sense, as how would the vector know if it should delete the items or if they might also be owned elsewhere ... if you want the objects to be managed by the vector, you need a smart-pointer or reference class, that will do so ... for instance I have a template class that implements a ref-counted smart owner for any object, and then I have a little wrapper template which automatically increments the ref count in copy constructor and assignment operators, decrements them during assignement operator and destructor. Good Luck
Yeah, i thought so.

--------------------
Though this program be madness, yet there is a method in''t
--------------------Though this program be madness, yet there is a method in't

This topic is closed to new replies.

Advertisement