473,766 Members | 2,035 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

STL and linked lists

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.
Jul 23 '05 #1
7 1787
On 2005-06-27, Sammy <di************ **@pht.zzz> 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/
Jul 23 '05 #2
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
Jul 23 '05 #3

"Sammy" <di************ **@pht.zzz> wrote in message
news:Xn******** **************@ 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.
Jul 23 '05 #4
"Jim Langston" <ta*******@rock etmail.com> wrote in
news:Lq******** *********@fe05. lga:
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)
Jul 23 '05 #5
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
CopyConstructib le and Assignable requirements for standard containers.
std::auto_ptr, for example, does not. boost::shared_p tr does.

-Alan
Jul 23 '05 #6


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

Jul 23 '05 #7
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
Jul 23 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
4828
by: Chris Ritchey | last post by:
Hmmm I might scare people away from this one just by the title, or draw people in with a chalange :) I'm writting this program in c++, however I'm using char* instead of the string class, I am ordered by my instructor and she does have her reasons so I have to use char*. So there is alot of c in the code as well Anyways, I have a linked list of linked lists of a class we defined, I need to make all this into a char*, I know that I...
10
15129
by: Kent | last post by:
Hi! I want to store data (of enemys in a game) as a linked list, each node will look something like the following: struct node { double x,y; // x and y position coordinates struct enemy *enemydata; // Holds information about an enemy (in a game) // Its a double linked list node
1
12844
by: Booser | last post by:
// Merge sort using circular linked list // By Jason Hall <booser108@yahoo.com> #include <stdio.h> #include <stdlib.h> #include <time.h> #include <math.h> //#define debug
12
2514
by: Jonathan Bartlett | last post by:
Just finished a new IBM DeveloperWorks article on linked lists, and thought you all might be interested. It's not an introduction -- it instead covers some of the more interesting aspects of linked lists. http://www.ibm.com/developerworks/linux/library/l-listproc/ Jon ---- Learn to program using Linux assembly language http://www.cafeshops.com/bartlettpublish.8640017
3
3273
by: s_subbarayan | last post by:
Dear all, 1)In one of our implementation for an application we are supposed to collate two linked lists.The actual problem is like this: There are two singularly linked lists, the final output will be a "perfectly shuffle" of the lists together into a single list. The new list should consist of consecutively alternating nodes from both lists.Note that both these linked lists have same kind of data structure in their nodes.
4
3600
by: MJ | last post by:
Hi I have written a prog for reversing a linked list I have used globle pointer Can any one tell me how I can modify this prog so that I dont have to use extra pointer Head1. When I reverse a LL using the recursive call how to change the last node pointer to head and head->next to null without using the extra global pointer Mayur
3
472
by: Little | last post by:
Could someone tell me what I am doing wrong here about declaring mutiple double linked lists. This is what the information is for the project and the code wil be below that. Thank your soo much for your assitance in helping me solve this problem. Information: Create 4 double linked lists as follows: (a) A double linked list called NAMES which will contain all C like
12
3953
by: joshd | last post by:
Hello, Im sorry if this question has been asked before, but I did search before posting and couldnt find an answer to my problem. I have two classes each with corresponding linked lists, list1 and list2, each node within list1 has various data and needs to have a pointer to the corresponding node in list2, but I cant figure out how to do this. Could someone explain what I might be missing, or maybe point me in the direction of a good...
19
7825
by: Dongsheng Ruan | last post by:
with a cell class like this: #!/usr/bin/python import sys class Cell: def __init__( self, data, next=None ): self.data = data
51
8647
by: Joerg Schoen | last post by:
Hi folks! Everyone knows how to sort arrays (e. g. quicksort, heapsort etc.) For linked lists, mergesort is the typical choice. While I was looking for a optimized implementation of mergesort for linked lists, I couldn't find one. I read something about Mcilroy's "Optimistic Merge Sort" and studied some implementation, but they were for arrays. Does anybody know if Mcilroys optimization is applicable to truly linked lists at all?
0
9568
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9404
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10168
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10008
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9837
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6651
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5279
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.