Does deleting a container of pointers also delete the (contained) pointers?

X

Xamalek

Greetings,

I have a question. When using an STL container, does deleting a container of
pointers also call delete on the (contained) pointers?

For example, if I have (ignore the fluff, it is simply used to explain my
issue)

struct Proc
{
int uid;
int pid;
int parent_id;
};

and then I have

queue<Proc*> Q = new queue<Proc*>;

// I then populate the queue with Proc types


If I do a

delete Q;

will that destroy the queue and the inner elements as well, or do I have to
do the work of ensuring that delete is also called on all the Proc*'s
(contained by the queue) as well?

Sincerely,
X
 
R

Rolf Magnus

Xamalek said:
Greetings,

I have a question. When using an STL container, does deleting a
container of pointers also call delete on the (contained) pointers?
No.


For example, if I have (ignore the fluff, it is simply used to explain
my issue)

struct Proc
{
int uid;
int pid;
int parent_id;
};

and then I have

queue<Proc*> Q = new queue<Proc*>;

// I then populate the queue with Proc types


If I do a

delete Q;

will that destroy the queue and the inner elements as well, or do I
have to do the work of ensuring that delete is also called on all the
Proc*'s (contained by the queue) as well?

The latter.
 
C

Cy Edmunds

Xamalek said:
Greetings,

I have a question. When using an STL container, does deleting a container of
pointers also call delete on the (contained) pointers?

No. Imagine if it did:

void disaster()
{
std::vector<int*> v;
int k;
v.push_back(&k);
} // undefined behavior here if std::vector deleted contained pointers

There is no way for the collection to tell if the pointer was obtained from
the new() operator or not.
For example, if I have (ignore the fluff, it is simply used to explain my
issue)

struct Proc
{
int uid;
int pid;
int parent_id;
};

and then I have

queue<Proc*> Q = new queue<Proc*>;

Huh? I think you mean:

queue<Proc*> *Q = new queue<Proc*>;

although why you would dynamically allocate it is beyond me.
// I then populate the queue with Proc types


If I do a

delete Q;

will that destroy the queue and the inner elements as well, or do I have to
do the work of ensuring that delete is also called on all the Proc*'s
(contained by the queue) as well?

You have to delete them yourself unless you use a reference counted smart
pointer.
 
E

Evan

Cy Edmunds said:
You have to delete them yourself unless you use a reference counted smart
pointer.

Most smart pointers should do really; I think the auto_ptr of the
standard library would work fine. For instance:
queue<std::auto_ptr<Proc> > *Q = new queue<std::auto_ptr<Proc> >;
...
delete Q;
should do the trick. You have to be careful with this though, make
sure you know what auto_ptr does before you use it, or you could
easily run into greater trouble than allowing memory leaks (which your
program does now by not deleting what the contents point to) by
accessing memory you think is good but is out of scope because the
auto_ptr went out of scope and deleted its subject.

Evan
 
K

Karl Heinz Buchegger

Evan said:
Most smart pointers should do really; I think the auto_ptr of the
standard library would work fine.

No. auto_ptr doesn't work. Its copy semantics are wrong.
But the smart pointers from the boost library will work fine.

www.boost.org
 
R

Rob Williscroft

Evan wrote in
Most smart pointers should do really; I think the auto_ptr of the
standard library would work fine. For instance:
queue<std::auto_ptr<Proc> > *Q = new queue<std::auto_ptr<Proc> >;
...

Unfortunatly you cant put std::auto_ptr<> into a standard container
as it doesn't meet the minimum requirments, for a containers value_type,
this because it moves rather than copies or assign's it contents.

look into boost shared_ptr if you want to do this:

http://www.boost.org/libs/smart_ptr/shared_ptr.htm
delete Q;
should do the trick. You have to be careful with this though, make
sure you know what auto_ptr does before you use it, or you could
easily run into greater trouble than allowing memory leaks (which your
program does now by not deleting what the contents point to) by
accessing memory you think is good but is out of scope because the
auto_ptr went out of scope and deleted its subject.

Rob.
 
E

Evan

Karl Heinz Buchegger said:
No. auto_ptr doesn't work. Its copy semantics are wrong.
But the smart pointers from the boost library will work fine.

www.boost.org

(In reply to Rob and Gavin too)

Doh, yes, I read about that. In a couple places. Including the GOTW
columns.

Yes, auto_ptr won't work because the containers copy around their
values internally. I forgot about that. Thanks guys.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top