STL and linked lists

S

Sammy

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

Donovan Rebbechi

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,
 
S

Steven T. Hatton

Sammy said:
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.
 
J

Jim Langston

Sammy said:
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.
 
S

Sammy

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)
 
A

Alan Johnson

Sammy said:
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
 
M

msalters

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
 
S

Steven T. Hatton

msalters said:
Sammy schreef:

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top