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. 7 1766
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/
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
"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.
"Jim Langston" <ta*******@rocketmail.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)
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
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 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |