Skip to main content
gamedev.net
🔒 Locked

using vector with pointers

Started by Great_White Aug 9, 2007 at 5:32 PM 4 replies 600+ views
Original Post
Great_White
Great_White
Hi, I am wondering something about STL vector class containing pointers. For example if we create something like :

class A
{
  int a;
  .
  .
};

std::vector< A* > array;

array.push_back(new A);
array.push_back(new A);
         .
         .
         .

Will the destructors for A* types be called automatically upon exit inside the destructor of vector ? Or should we delete them manually?
"A human being is a part of a whole, called by us,universe, a part limited in time and space. He experiences himself, his thoughts and feelings as something separated from the rest... a kind of optical delusion of his consciousness. This delusion is a kind of prison for us, restricting us to our personal desires and to affection for a few persons nearest to us. Our task must be to free ourselves from this prison by widening our circle of compassion to embrace all living creatures and the whole o
SiCrane
SiCrane
If you new it, you should delete it. std::vector won't do it for you.
TheUnbeliever
TheUnbeliever
The vector will call the destructors for the objects it knows about (i.e. the type it contains). In this case this is the pointers, and so you have to call delete on them manually before using array.clear() to remove the pointers themselves.

However, using a smart pointer such as boost::shared_ptr (which, I think, is being integrated into the next version of the C++ standard; although, I may be very wrong about that) can alleviate some of the manual memory management concerns.

I also think there's something like boost::ptr_vector which might suit your purposes, but which I haven't used.
[TheUnbeliever]
Great_White
Great_White
So, I need to manually delete them like this way:

for(int i=0; i<array.size(); ++i)   delete array;

"A human being is a part of a whole, called by us,universe, a part limited in time and space. He experiences himself, his thoughts and feelings as something separated from the rest... a kind of optical delusion of his consciousness. This delusion is a kind of prison for us, restricting us to our personal desires and to affection for a few persons nearest to us. Our task must be to free ourselves from this prison by widening our circle of compassion to embrace all living creatures and the whole o
Zakwayda
Zakwayda
Quote:
Original post by Great_White
So, I need to manually delete them like this way:

*** Source Snippet Removed ***
Yup, but it would be better to use shared_ptr's (or ptr_vector) as previously suggested. (Also, if you want to stick with manual deletion as in your above example, you can make the code cleaner and more general by using e.g. std::for_each().)
Great_White
Great_White
OK guys thx for your help.
"A human being is a part of a whole, called by us,universe, a part limited in time and space. He experiences himself, his thoughts and feelings as something separated from the rest... a kind of optical delusion of his consciousness. This delusion is a kind of prison for us, restricting us to our personal desires and to affection for a few persons nearest to us. Our task must be to free ourselves from this prison by widening our circle of compassion to embrace all living creatures and the whole o

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.