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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Xamalek

    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


  • Rolf Magnus

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

    Xamalek wrote:
    [color=blue]
    > Greetings,
    >
    > I have a question. When using an STL container, does deleting a
    > container of pointers also call delete on the (contained) pointers?[/color]

    No.
    [color=blue]
    >
    > 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?[/color]

    The latter.

    Comment

    • Cy Edmunds

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

      "Xamalek" <xamalek-nospam-spammm@yahoo.co m> wrote in message
      news:hgBpb.1332 5$Vg7.6043@news svr14.news.prod igy.com...[color=blue]
      > Greetings,
      >
      > I have a question. When using an STL container, does deleting a container[/color]
      of[color=blue]
      > pointers also call delete on the (contained) pointers?[/color]

      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.
      [color=blue]
      >
      > 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*>;[/color]

      Huh? I think you mean:

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

      although why you would dynamically allocate it is beyond me.
      [color=blue]
      >
      > // 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[/color]
      to[color=blue]
      > do the work of ensuring that delete is also called on all the Proc*'s
      > (contained by the queue) as well?[/color]

      You have to delete them yourself unless you use a reference counted smart
      pointer.
      [color=blue]
      >
      > Sincerely,
      > X
      >
      >[/color]

      --
      Cy



      Comment

      • Evan

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

        "Cy Edmunds" <cedmunds@spaml ess.rochester.r r.com> wrote in message news:<1aEpb.466 27$ZC4.22313@tw ister.nyroc.rr. com>...[color=blue]
        > You have to delete them yourself unless you use a reference counted smart
        > pointer.[/color]

        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

        Comment

        • Karl Heinz Buchegger

          #5
          Re: Does deleting a container of pointers also delete the (contained)poin ters?



          Evan wrote:[color=blue]
          >
          > "Cy Edmunds" <cedmunds@spaml ess.rochester.r r.com> wrote in message news:<1aEpb.466 27$ZC4.22313@tw ister.nyroc.rr. com>...[color=green]
          > > You have to delete them yourself unless you use a reference counted smart
          > > pointer.[/color]
          >
          > Most smart pointers should do really; I think the auto_ptr of the
          > standard library would work fine.[/color]

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



          --
          Karl Heinz Buchegger
          kbuchegg@gascad .at

          Comment

          • Rob Williscroft

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

            Evan wrote in news:3f25c666.0 311032302.5654b 081@posting.goo gle.com:
            [color=blue]
            > "Cy Edmunds" <cedmunds@spaml ess.rochester.r r.com> wrote in message
            > news:<1aEpb.466 27$ZC4.22313@tw ister.nyroc.rr. com>...[color=green]
            >> You have to delete them yourself unless you use a reference counted
            >> smart pointer.[/color]
            >
            > 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> >;
            > ...[/color]

            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:


            [color=blue]
            > 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.
            >[/color]

            Rob.
            --

            Comment

            • Gavin Deane

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

              eed132@psu.edu (Evan) wrote in message news:<3f25c666. 0311032302.5654 b081@posting.go ogle.com>...[color=blue]
              > "Cy Edmunds" <cedmunds@spaml ess.rochester.r r.com> wrote in message news:<1aEpb.466 27$ZC4.22313@tw ister.nyroc.rr. com>...[color=green]
              > > You have to delete them yourself unless you use a reference counted smart
              > > pointer.[/color]
              >
              > 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;[/color]

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

              hth
              GJD

              Comment

              • Evan

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

                Karl Heinz Buchegger <kbuchegg@gasca d.at> wrote in message news:<3FA76FD4. 88540ED2@gascad .at>...[color=blue]
                > Evan wrote:[color=green]
                > >
                > > "Cy Edmunds" <cedmunds@spaml ess.rochester.r r.com> wrote in message news:<1aEpb.466 27$ZC4.22313@tw ister.nyroc.rr. com>...[color=darkred]
                > > > You have to delete them yourself unless you use a reference counted smart
                > > > pointer.[/color]
                > >
                > > Most smart pointers should do really; I think the auto_ptr of the
                > > standard library would work fine.[/color]
                >
                > 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[/color]

                (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.

                Comment

                Working...