STL and linked lists

Discussion in 'C++' started by Sammy, Jun 27, 2005.

  1. Sammy

    Sammy Guest

    Its slow but Im in the process of converting some of my admittedly ugly
    code into STL. At the moment Im working to change a mess that I like to
    call a linked list when Im in a generous mood. Actually it works, but could
    be much better.

    When using the STL list (made up of simple pointers), I assume using the
    erase function does not automatically destroy the objects they point to...
    just the pointer itself. Is this so?

    If it is... will using the erase function call up the destructor of
    whatever is being erased? Say if the list was made up of smart pointers
    that will handle deletions on their own?

    Thanks.
     
    Sammy, Jun 27, 2005
    #1
    1. Advertising

  2. On 2005-06-27, Sammy <> wrote:

    > When using the STL list (made up of simple pointers), I assume using the
    > erase function does not automatically destroy the objects they point to...
    > just the pointer itself. Is this so?


    Right. (If you think about it, it's unsafe to make calling delete on the
    pointers on by default)

    > If it is... will using the erase function call up the destructor of
    > whatever is being erased? Say if the list was made up of smart pointers
    > that will handle deletions on their own?


    If the list is made up of smart pointers, the destructors will get called
    In fact I don't see how you could prevent such destructors getting called
    without either making all lists leak, or writing "evil" code.

    Cheers,
    --
    Donovan Rebbechi
    http://pegasus.rutgers.edu/~elflord/
     
    Donovan Rebbechi, Jun 27, 2005
    #2
    1. Advertising

  3. Sammy wrote:

    > Its slow but Im in the process of converting some of my admittedly ugly
    > code into STL. At the moment Im working to change a mess that I like to
    > call a linked list when Im in a generous mood. Actually it works, but
    > could be much better.
    >
    > When using the STL list (made up of simple pointers), I assume using the
    > erase function does not automatically destroy the objects they point to...
    > just the pointer itself. Is this so?


    No. Erase will delete the element it's called on.

    > If it is... will using the erase function call up the destructor of
    > whatever is being erased? Say if the list was made up of smart pointers
    > that will handle deletions on their own?


    Not sure what you mean by "handle deletion on their own". It will call
    delete on the smart pointer, what happens to the subject of that pointer is
    a different question.
    --
    If our hypothesis is about anything and not about some one or more
    particular things, then our deductions constitute mathematics. Thus
    mathematics may be defined as the subject in which we never know what we
    are talking about, nor whether what we are saying is true.-Bertrand Russell
     
    Steven T. Hatton, Jun 27, 2005
    #3
  4. Sammy

    Jim Langston Guest

    "Sammy" <> wrote in message
    news:Xns9681CDE53CA84kevsmail@216.168.3.44...
    > Its slow but Im in the process of converting some of my admittedly ugly
    > code into STL. At the moment Im working to change a mess that I like to
    > call a linked list when Im in a generous mood. Actually it works, but
    > could
    > be much better.
    >
    > When using the STL list (made up of simple pointers), I assume using the
    > erase function does not automatically destroy the objects they point to...
    > just the pointer itself. Is this so?
    >
    > If it is... will using the erase function call up the destructor of
    > whatever is being erased? Say if the list was made up of smart pointers
    > that will handle deletions on their own?
    >
    > Thanks.


    If you declared a list of pointers, and assigned somethign to these pointers
    using
    new you need to explicitly call delete on each one before you erase the
    list.
    simply iterate through the list and call delete for each element, then you
    can do you
    erase.

    Using smart pointers (which I've never used) I understand that they will
    delete themselves
    when required,.so you don't have to iterate though the list and delete smart
    pointers.
     
    Jim Langston, Jun 27, 2005
    #4
  5. Sammy

    Sammy Guest

    "Jim Langston" <> wrote in
    news:LqLve.7082$:

    > Using smart pointers (which I've never used) I understand that they
    > will delete themselves
    > when required,.so you don't have to iterate though the list and delete
    > smart pointers.


    I've used them before in a small way, never really delving into them. But
    after reading various online references further and thumbing through more
    pages, I wonder if I might be making things more difficult than I need to.
    Just pass the class directly when adding to the list instead of passing a
    pointer to one that I create myself?

    As far as I can tell the memory leaks I had were fixed. At least I didnt
    have any errors as such, But I probably do have some 'evil' code here and
    there that I just never really saw before. This is what I mean by
    'messy' ;o)
     
    Sammy, Jun 27, 2005
    #5
  6. Sammy

    Alan Johnson Guest

    Sammy wrote:
    > If it is... will using the erase function call up the destructor of
    > whatever is being erased? Say if the list was made up of smart pointers
    > that will handle deletions on their own?


    This is correct, but be careful that your smart pointer meets the
    CopyConstructible and Assignable requirements for standard containers.
    std::auto_ptr, for example, does not. boost::shared_ptr does.

    -Alan
     
    Alan Johnson, Jun 27, 2005
    #6
  7. Sammy

    msalters Guest

    Sammy schreef:
    > Its slow but Im in the process of converting some of my admittedly ugly
    > code into STL. At the moment Im working to change a mess that I like to
    > call a linked list when Im in a generous mood. Actually it works, but could
    > be much better.
    >
    > When using the STL list (made up of simple pointers), I assume using the
    > erase function does not automatically destroy the objects they point to...
    > just the pointer itself. Is this so?


    Of course. I wouldn't like std::list<char const*> to call delete on
    my "" string literal. Or on the return value of malloc(). It only calls
    char const*::~char const*, which is a do-nothing pseudo-destructor.

    (Pseudo-destructors were added just so you can say T::~T for every T,
    even built-in types like int)

    HTH,
    Michiel Salters
     
    msalters, Jun 27, 2005
    #7
  8. msalters wrote:

    >
    >
    > Sammy schreef:
    >> Its slow but Im in the process of converting some of my admittedly ugly
    >> code into STL. At the moment Im working to change a mess that I like to
    >> call a linked list when Im in a generous mood. Actually it works, but
    >> could be much better.
    >>
    >> When using the STL list (made up of simple pointers), I assume using the
    >> erase function does not automatically destroy the objects they point
    >> to... just the pointer itself. Is this so?

    >
    > Of course. I wouldn't like std::list<char const*> to call delete on
    > my "" string literal. Or on the return value of malloc(). It only calls
    > char const*::~char const*, which is a do-nothing pseudo-destructor.
    >
    > (Pseudo-destructors were added just so you can say T::~T for every T,
    > even built-in types like int)
    >
    > HTH,
    > Michiel Salters


    I now realize I did not understand the question. I was understanding the
    pointers to be the ones used to implement the list, no what the list
    contains. I hope my reply wasn't too confusing.
    --
    If our hypothesis is about anything and not about some one or more
    particular things, then our deductions constitute mathematics. Thus
    mathematics may be defined as the subject in which we never know what we
    are talking about, nor whether what we are saying is true.-Bertrand Russell
     
    Steven T. Hatton, Jun 27, 2005
    #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. Chris Ritchey
    Replies:
    7
    Views:
    514
    emerth
    Jul 10, 2003
  2. Chris Ritchey

    Generating a char* from a linked list of linked lists

    Chris Ritchey, Jul 9, 2003, in forum: C Programming
    Replies:
    7
    Views:
    503
    emerth
    Jul 10, 2003
  3. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    List of lists of lists of lists...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006, in forum: Python
    Replies:
    5
    Views:
    445
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
  4. fool
    Replies:
    14
    Views:
    546
    Barry Schwarz
    Jul 3, 2006
  5. jawdoc
    Replies:
    9
    Views:
    808
    Chris Thomasson
    Mar 10, 2008
Loading...

Share This Page