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

Discussion in 'C++' started by Xamalek, Nov 3, 2003.

  1. Xamalek

    Xamalek Guest

    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
    Xamalek, Nov 3, 2003
    #1
    1. Advertising

  2. Xamalek

    Rolf Magnus Guest

    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.
    Rolf Magnus, Nov 3, 2003
    #2
    1. Advertising

  3. Xamalek

    Cy Edmunds Guest

    "Xamalek" <> wrote in message
    news:hgBpb.13325$...
    > 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/
    Cy Edmunds, Nov 4, 2003
    #3
  4. Xamalek

    Evan Guest

    "Cy Edmunds" <> wrote in message news:<1aEpb.46627$>...
    > 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
    Evan, Nov 4, 2003
    #4
  5. Re: Does deleting a container of pointers also delete the (contained)pointers?

    Evan wrote:
    >
    > "Cy Edmunds" <> wrote in message news:<1aEpb.46627$>...
    > > 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
    Karl Heinz Buchegger, Nov 4, 2003
    #5
  6. Evan wrote in news::

    > "Cy Edmunds" <> wrote in message
    > news:<1aEpb.46627$>...
    >> 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/
    Rob Williscroft, Nov 4, 2003
    #6
  7. Xamalek

    Gavin Deane Guest

    (Evan) wrote in message news:<>...
    > "Cy Edmunds" <> wrote in message news:<1aEpb.46627$>...
    > > 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
    Gavin Deane, Nov 4, 2003
    #7
  8. Xamalek

    Evan Guest

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

    Karl Heinz Buchegger <> wrote in message news:<>...
    > Evan wrote:
    > >
    > > "Cy Edmunds" <> wrote in message news:<1aEpb.46627$>...
    > > > 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.
    Evan, Nov 4, 2003
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?dmNvcm5qYW1i?=
    Replies:
    3
    Views:
    1,093
    Dan Bass
    Nov 29, 2004
  2. Replies:
    5
    Views:
    2,978
    Earl Purple
    Dec 18, 2005
  3. morz
    Replies:
    5
    Views:
    341
  4. Replies:
    2
    Views:
    395
  5. Peter Olcott
    Replies:
    36
    Views:
    1,059
    Daniel Pitts
    Jun 20, 2008
Loading...

Share This Page