STL: delete needed along with erase?

Discussion in 'C++' started by AbrahamLincolnIllinois@yahoo.com, Jul 9, 2007.

  1. Guest

    Hi all.

    I have a list of pointers to a complicated object. When I erase() a
    member of that list, the little blob of memory that contains the
    pointer is deleted, I think. But the object pointed to is not
    deleted, true? It is not an auto_ptr or any kind of wrapped
    pointer.

    Further, I assume that the iterator I am holding is no longer valid
    once I delete the element. I can't safely erase the member first,
    then delete the object through the iterator. So the sequence ought to
    be: 1. Delete the object. 2. Erase the list element.

    I am checking my understanding, because the code I am working on,
    which I inherited, doesn't delete the object at all, either before or
    after the element is erased.


    Abraham
    , Jul 9, 2007
    #1
    1. Advertising

  2. Re: delete needed along with erase?

    wrote:
    > I have a list of pointers to a complicated object. When I erase() a
    > member of that list, the little blob of memory that contains the
    > pointer is deleted, I think.


    Whatever the implementation does to get rid of it, yes.

    > But the object pointed to is not
    > deleted, true?


    True.

    > It is not an auto_ptr or any kind of wrapped
    > pointer.


    Right. You cannot use 'auto_ptr', but 'shared_ptr' should be fine.

    > Further, I assume that the iterator I am holding is no longer valid
    > once I delete the element.


    You mean, once you *erase* it? "Delete" has a particular meaning in
    C++, as you know. Yes, the iterator is invalid if you erase the list
    element behind it.

    > I can't safely erase the member first,
    > then delete the object through the iterator. So the sequence ought to
    > be: 1. Delete the object. 2. Erase the list element.


    That sounds right.

    > I am checking my understanding, because the code I am working on,
    > which I inherited, doesn't delete the object at all, either before or
    > after the element is erased.


    Good thinking.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Jul 9, 2007
    #2
    1. Advertising

  3. Guest

    Re: delete needed along with erase?

    Thanks, Victor.

    That's what I needed to know.


    Abraham


    On Jul 9, 9:22 am, "Victor Bazarov" <> wrote:
    > wrote:
    > > I have a list of pointers to a complicated object. When I erase() a
    > > member of that list, the little blob of memory that contains the
    > > pointer is deleted, I think.

    >
    > Whatever the implementation does to get rid of it, yes.
    >


    : :
    , Jul 9, 2007
    #3
  4. Default User Guest

    Re: delete needed along with erase? - TPA

    wrote:

    > Thanks, Victor.


    Please don't top-post. Your replies belong following or interspersed
    with properly trimmed quotes. See the majority of other posts in the
    newsgroup, or the group FAQ list:
    <http://www.parashift.com/c++-faq-lite/how-to-post.html>
    Default User, Jul 9, 2007
    #4
    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. John Harrison

    Re: STL insert/erase behaviour

    John Harrison, Aug 13, 2003, in forum: C++
    Replies:
    0
    Views:
    628
    John Harrison
    Aug 13, 2003
  2. Andrew Koenig

    Re: STL insert/erase behaviour

    Andrew Koenig, Aug 13, 2003, in forum: C++
    Replies:
    0
    Views:
    372
    Andrew Koenig
    Aug 13, 2003
  3. Paras

    stl list erase

    Paras, Aug 20, 2003, in forum: C++
    Replies:
    13
    Views:
    3,776
    Alan Chen
    Aug 21, 2003
  4. s

    STL list::erase()

    s, Sep 15, 2003, in forum: C++
    Replies:
    2
    Views:
    7,263
    Rob Williscroft
    Sep 15, 2003
  5. erase vs. erase

    , Mar 25, 2006, in forum: C++
    Replies:
    7
    Views:
    351
    Pete Becker
    Mar 30, 2006
Loading...

Share This Page