473,326 Members | 2,108 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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

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
Jul 19 '05 #1
7 3543
Xamalek wrote:
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.

Jul 19 '05 #2
"Xamalek" <xa*******************@yahoo.com> wrote in message
news:hg******************@newssvr14.news.prodigy.c om...
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.

Sincerely,
X


--
Cy
http://home.rochester.rr.com/cyhome/
Jul 19 '05 #3
"Cy Edmunds" <ce******@spamless.rochester.rr.com> wrote in message news:<1a*******************@twister.nyroc.rr.com>. ..
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
Jul 19 '05 #4


Evan wrote:

"Cy Edmunds" <ce******@spamless.rochester.rr.com> wrote in message news:<1a*******************@twister.nyroc.rr.com>. ..
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.


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

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #5
Evan wrote in news:3f**************************@posting.google.c om:
"Cy Edmunds" <ce******@spamless.rochester.rr.com> wrote in message
news:<1a*******************@twister.nyroc.rr.com>. ..
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> >;
...


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.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '05 #6
ee****@psu.edu (Evan) wrote in message news:<3f**************************@posting.google. com>...
"Cy Edmunds" <ce******@spamless.rochester.rr.com> wrote in message news:<1a*******************@twister.nyroc.rr.com>. ..
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;


Don't do that. See http://www.gotw.ca/gotw/025.htm

hth
GJD
Jul 19 '05 #7
Karl Heinz Buchegger <kb******@gascad.at> wrote in message news:<3F***************@gascad.at>...
Evan wrote:

"Cy Edmunds" <ce******@spamless.rochester.rr.com> wrote in message news:<1a*******************@twister.nyroc.rr.com>. ..
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.


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.
Jul 19 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Hanzo | last post by:
I'm iterating over a vector of base class pointers and deleting those which meet a certain criteria...i'm using pretty text-book code for the particular delete/erasure (it's straight out of Myers'...
8
by: Nobody | last post by:
The requirement that STL container elements have to be assignable is causing me a problem. Consider a class X which contains both const and non-const data members: class X { public: X(const...
6
by: Matan Nassau | last post by:
Hello. i have a composite which i want to delete. this is a composite which represents a boolean expression (see a previous post of mine with more details at...
9
by: Aguilar, James | last post by:
Hey guys. A new question: I want to use an STL libarary to hold a bunch of objects I create. Actually, it will hold references to the objects, but that's beside the point, for the most part. ...
5
by: Tommo | last post by:
I am storing some key value pairs in a map as follows: <code> bool StaticDataCache::putMarket(uint8_t key,item_t* value) { typedef pair<uint8_t,item_t*> entry; typedef...
5
by: edward.birch | last post by:
Can anyone see anything wrong with the following code? (CONTAINER can be list, vector, set, ...) template <class T> void Destroy(T * p) { delete p; } void...
14
by: PengYu.UT | last post by:
In the following program, I want an iterator contain pointer pointing to constant object not const pointer. If it is possible would you please let me know how to do it? #include...
19
by: Angus | last post by:
I have a socket class CTestClientSocket which I am using to simulate load testing. I create multiple instances of the client like this: for (int i = 0; i < 5; i++) { CTestClientSocket* pTemp...
3
by: deepak1905 | last post by:
Hi, I am working on c++ in a linux system ( Fedora core 4 ), kernel version - 2.6.11-1.1369_FC4 gcc version - 4.0.0 20050519 ( Red Hat 4.0.0-8 ) In my code i am creating a vector to store...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.