Hello
I have a linked list of object of a class. I thought it would be nice to
have the destructor delete the whole list when I delete just the first
element. I don't want to recursivly destroy the list elements; the list is
arbitrary long. Something like this:
class ListElem
{
char Datum[1000];
class ListElem *Next;
ListElem()
{ Next = NULL; }
~ListElem()
{
class ListElem *el;
while (Next)
{
el = Next;
Next = Next->Next;
delete el;
}
}
}
but the last instruction delete el; will make a recursive call to the
destructor and it's all going down.
How can I delete an element, from the destructor, without calling the
destructor again ?
Thank you
Timothy Madden
Romania
---------------------------------------------
And I don't wanna miss a thing 14 2380
"Timothy Madden" <ba****@rmv.spam.home.ro> wrote in message
news:2r*************@uni-berlin.de... Hello
I have a linked list of object of a class. I thought it would be nice to have the destructor delete the whole list when I delete just the first element. I don't want to recursivly destroy the list elements; the list is arbitrary long. Something like this:
I'm not sure what you mean when you say you don't want to "recursively"
destroy the elements. Generally, you'd use a loop to delete a list's
elements, not recursion.
(Now, if you *wanted* recursion, you could have the destructor just call
"delete Next;", so that each element deleted the following element. The
last one would be calling delete on a NULL pointer, which is a no-op, and
would end the recursion.) class ListElem { char Datum[1000]; class ListElem *Next; ListElem() { Next = NULL; } ~ListElem() { class ListElem *el; while (Next) { el = Next; Next = Next->Next; delete el; } } }
but the last instruction delete el; will make a recursive call to the destructor and it's all going down.
How can I delete an element, from the destructor, without calling the destructor again ?
I don't think this idea, even if you can find a way to do it, would be a
very good way to do things. Why not use a loop *outside* the destructor, to
destroy the list elements. The best place for it would be in the destructor
of the class that encapsulates the list itself (i.e., the one that owns the
pointer to the first element in the list). If you just have this list
residing in main, and not in a class, I'd recommend putting it in a class
instead, and then putting the loop in that class' destructor.
-Howard
Timothy Madden wrote: I have a linked list of object of a class. I thought it would be nice to have the destructor delete the whole list when I delete just the first element. I don't want to recursivly destroy the list elements; the list is arbitrary long. Something like this:
class ListElem { char Datum[1000]; class ListElem *Next;
^^^^^
No need for keyword 'class' here.
ListElem() { Next = NULL; } ~ListElem() { class ListElem *el;
^^^^^
No need for keyword 'class' here either.
while (Next) { el = Next; Next = Next->Next; delete el; } } }
but the last instruction delete el; will make a recursive call to the destructor and it's all going down.
A recursive call to the destructor is no big deal, AFAICT. To avoid
multiple deletions (I presume that's what you're experiencing) you do
~ListElem()
{
delete Next;
}
That should take care of the whole list. If the element being deleted
has 'Next' a null pointer, 'delete 0' does nothing and your recursion
returns.
How can I delete an element, from the destructor, without calling the destructor again ?
Victor
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:qR****************@newsread1.dllstx09.us.to.v erio.net... Timothy Madden wrote: class ListElem { char Datum[1000]; class ListElem *Next; ListElem() { Next = NULL; } ~ListElem() { class ListElem *el; while (Next) { el = Next; Next = Next->Next; delete el; } } }
but the last instruction delete el; will make a recursive call to the destructor and it's all going down.
A recursive call to the destructor is no big deal, AFAICT. To avoid multiple deletions (I presume that's what you're experiencing) you do
~ListElem() { delete Next; }
That should take care of the whole list. If the element being deleted has 'Next' a null pointer, 'delete 0' does nothing and your recursion returns.
I don't want to recursively destroy the list; the list is arbitrary long.
Any 'delete Next' in the desctructor will recursivly call the destructor;
What if the list is 10000 or 100000 or 1000000 elements ?
"Howard" <al*****@hotmail.com> wrote in message
news:bQ*********************@bgtnsc05-news.ops.worldnet.att.net... "Timothy Madden" <ba****@rmv.spam.home.ro> wrote in message news:2r*************@uni-berlin.de... Hello
I have a linked list of object of a class. I thought it would be nice to have the destructor delete the whole list when I delete just the first element. I don't want to recursivly destroy the list elements; the list
is arbitrary long. Something like this: I'm not sure what you mean when you say you don't want to "recursively" destroy the elements. Generally, you'd use a loop to delete a list's elements, not recursion.
I mean I don't wanna do
ListElem::~ListElem()
{
delete Next;
}
and that I want a loop. But in a loop I still have to use delete and that's
the
problem. How can I delete an element, from the destructor, without calling the destructor again ? I don't think this idea, even if you can find a way to do it, would be a very good way to do things. Why not use a loop *outside* the destructor,
to destroy the list elements. The best place for it would be in the
destructor of the class that encapsulates the list itself (i.e., the one that owns
the pointer to the first element in the list). If you just have this list residing in main, and not in a class, I'd recommend putting it in a class instead, and then putting the loop in that class' destructor.
There is no class that encapsulates the list. All you need is an element of
the list and you really have a list. I hope you agree it is much simple like
this.
Of course a haviley used list needs a santinel for performance and so it
needs an encapsulating class but right now I just need to do things and to
do them simple.
That is, when I'll write the proper destructor ...
Timothy Madden wrote: "Victor Bazarov" <v.********@comAcast.net> wrote in message news:qR****************@newsread1.dllstx09.us.to.v erio.net...
Timothy Madden wrote:
class ListElem { char Datum[1000]; class ListElem *Next; ListElem() { Next = NULL; } ~ListElem() { class ListElem *el; while (Next) { el = Next; Next = Next->Next; delete el; } } }
but the last instruction delete el; will make a recursive call to the destructor and it's all going down.
A recursive call to the destructor is no big deal, AFAICT. To avoid multiple deletions (I presume that's what you're experiencing) you do
~ListElem() { delete Next; }
That should take care of the whole list. If the element being deleted has 'Next' a null pointer, 'delete 0' does nothing and your recursion returns.
I don't want to recursively destroy the list; the list is arbitrary long. Any 'delete Next' in the desctructor will recursivly call the destructor; What if the list is 10000 or 100000 or 1000000 elements ?
You probably don't understand the importance of destructors. They define
the lifetime of objects. You cannot dispose of an object without invoking
or calling its destructor.
If you don't want to recursively destruct your list, don't define it the
way you defined it: recursively. In your list 'ListElem' is the _head_
of the list, and ListElem::Next is the _head_of_the_tail_ of that list.
ListElem essentially _is_ the list. And since destroying the list means
destroying its head _plus_ destroying its tail, that's what you have to
do. If your list is arbitrarily long, it still all has to be destroyed.
Victor
"Timothy Madden" <ba****@rmv.spam.home.ro> escribió en el mensaje
news:2r*************@uni-berlin.de... Hello
I have a linked list of object of a class. I thought it would be nice to have the destructor delete the whole list when I delete just the first element. I don't want to recursivly destroy the list elements; the list is arbitrary long. Something like this:
class ListElem { char Datum[1000]; class ListElem *Next; ListElem() { Next = NULL; } ~ListElem() { class ListElem *el; while (Next) { el = Next; Next = Next->Next; delete el; } } }
but the last instruction delete el; will make a recursive call to the destructor and it's all going down.
How can I delete an element, from the destructor, without calling the destructor again ?
Thank you Timothy Madden Romania --------------------------------------------- And I don't wanna miss a thing
I agree with Victor Bazarov, in the fact that your problem comes from a bad
design of your list. Yes, it seems to be simpler, faster and clearer, but
that is not truth, and your problem is enough prove of this. The
(simple-linked) list is an entity itself, and so it should be implemented as
a class other than ListElem. Though an element provides the functionality to
organize it inside a list, it's not the entire list, but just a single
element.
Also, you should consider a third class, an iterator, which manages a single
traversal over the list. Because you can have an arbitrary number of
simultaneous traversals over a same list, this functionality can not be
included inside the list itself (and again, it's not responsibility of a
single element).
Last, think that a single-linked list is one of the simplest data
structures. Things I comment here gain in relevance as the complexity levels
grows up.
But returning to your question, here is a solution which deletes the entire
list in the way that you want to do it, avoiding those problems derived from
recursion. There is still a recursion, which is unavoidable with your
scheme, but it's only a two-level recursion.
~ListElem()
{
ListElem * el;
while (Next)
{
el = Next;
Next = Next->Next;
el->Next = NULL; // This is enough to ensure that the list will
be not recursively traversed.
delete el;
}
}
> There is no class that encapsulates the list. All you need is an element of the list and you really have a list. I hope you agree it is much simple like this. Of course a haviley used list needs a santinel for performance and so it needs an encapsulating class but right now I just need to do things and to do them simple.
Not at all, its bad design. It causes confusion between operations on the
nodes of the list (which the user should not have to care about) and
operations on the list itself (which the user does care about). In fact this
is exactly the confusion you have yourself and you are writing the list!
Write a class that encapsulates the list, this is what OO is all about.
john
>> I don't want to recursively destroy the list; the list is arbitrary long. Any 'delete Next' in the desctructor will recursivly call the destructor; What if the list is 10000 or 100000 or 1000000 elements ?
You probably don't understand the importance of destructors. They define the lifetime of objects. You cannot dispose of an object without invoking or calling its destructor.
If you don't want to recursively destruct your list, don't define it the way you defined it: recursively. In your list 'ListElem' is the _head_ of the list, and ListElem::Next is the _head_of_the_tail_ of that list.
ListElem essentially _is_ the list. And since destroying the list means destroying its head _plus_ destroying its tail, that's what you have to do. If your list is arbitrarily long, it still all has to be destroyed.
Victor
I think the OP is worried about blowing the stack during list destruction.
john
On Friday 17 September 2004 12:16 pm, John Harrison did deign to grace us
with the following: I don't want to recursively destroy the list; the list is arbitrary long. Any 'delete Next' in the desctructor will recursivly call the destructor; What if the list is 10000 or 100000 or 1000000 elements ?
You probably don't understand the importance of destructors. They define the lifetime of objects. You cannot dispose of an object without invoking or calling its destructor.
If you don't want to recursively destruct your list, don't define it the way you defined it: recursively. In your list 'ListElem' is the _head_ of the list, and ListElem::Next is the _head_of_the_tail_ of that list.
ListElem essentially _is_ the list. And since destroying the list means destroying its head _plus_ destroying its tail, that's what you have to do. If your list is arbitrarily long, it still all has to be destroyed.
Victor
I think the OP is worried about blowing the stack during list destruction.
Then, wouldn't that be an additional reason to put it in a class?
Thanks,
Rich
>> I think the OP is worried about blowing the stack during list destruction. Then, wouldn't that be an additional reason to put it in a class?
Absolutely.
john
"Timothy Madden" <ba****@rmv.spam.home.ro> schrieb im Newsbeitrag news:2r*************@uni-berlin.de... Hello
I have a linked list of object of a class. I thought it would be nice to have the destructor delete the whole list when I delete just the first element. I don't want to recursivly destroy the list elements; the list is arbitrary long. Something like this:
class ListElem { char Datum[1000]; class ListElem *Next; ListElem() { Next = NULL; } ~ListElem() { class ListElem *el; while (Next) { el = Next; Next = Next->Next;
Insert
el->Next = 0;
here. Could be very useful :-)
delete el; } } }
but the last instruction delete el; will make a recursive call to the destructor and it's all going down.
How can I delete an element, from the destructor, without calling the destructor again ?
Thank you Timothy Madden Romania --------------------------------------------- And I don't wanna miss a thing
"Rubén Campos" <Ru**********@robotica.uv.es> wrote in message
news:ci**********@peque.uv.es... "Timothy Madden" <ba****@rmv.spam.home.ro> escribió en el mensaje news:2r*************@uni-berlin.de... Hello
I have a linked list of object of a class. I thought it would be nice to have the destructor delete the whole list when I delete just the first element. I don't want to recursivly destroy the list elements; the list
is arbitrary long. Something like this:
class ListElem { char Datum[1000]; class ListElem *Next; ListElem() { Next = NULL; } ~ListElem() { class ListElem *el; while (Next) { el = Next; Next = Next->Next; delete el; } } }
but the last instruction delete el; will make a recursive call to the destructor and it's all going down.
--------------------------------------------- ~ListElem() { ListElem * el;
while (Next) { el = Next; Next = Next->Next; el->Next = NULL; // This is enough to ensure that the list will be not recursively traversed. delete el; } }
Indeed it is so !!!!!!
Thank you very much !!
By myself, I ended up with a static flag inside the
destructor to stop recursion.
But el->Next = NULL is the real answer
Thank you
Timothy Madden
Romania
---------------------------------
And I don't wanna miss a thing
"R.ealname" <e.****@adres.se> wrote: "Timothy Madden" <ba****@rmv.spam.home.ro>: while (Next) { el = Next; Next = Next->Next;
Insert el->Next = 0;
here. Could be very useful :-)
delete el; }
Not. 'el' is deleted in the very next instruction, so that assignment
is worthless, a good compiler would optimise it out.
Old Wolf wrote: "R.ealname" <e.****@adres.se> wrote:
"Timothy Madden" <ba****@rmv.spam.home.ro>:
while (Next) { el = Next; Next = Next->Next;
Insert el->Next = 0;
here. Could be very useful :-)
delete el; }
Not. 'el' is deleted in the very next instruction, so that assignment is worthless, a good compiler would optimise it out.
It is often useful to read the entire thread before barging in. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Newsnet Customer |
last post by:
Hi,
Statement 1: "A dynamically created local object will call it's destructor
method when it goes out of scope when a procedure returms"
Agree.
Statement 2: "A dynamically created object...
|
by: Ioannis Vranos |
last post by:
When we use the standard placement new operator provided in <new>, and
not a definition of owr own, isn't a call to placement delete enough?
Consider the code:
#include <new>
|
by: Jonan |
last post by:
Hello,
For several reasons I want to replace the built-in memory management with
some custom built. The mem management itlsef is not subject to my question -
it's ok to the point that I have...
|
by: Douglas Peterson |
last post by:
class Allocator
{
public:
virtual void * Alloc(size_t) = 0;
virtual void * Free(void*) = 0;
};
class Object
{
public:
|
by: n2xssvv g02gfr12930 |
last post by:
In a job interview I was asked about the statement below:
delete this;
Naturally I was horrified, yet they claimed they had used it.
Personally I'm pretty damn sure I could never justify this....
|
by: junw2000 |
last post by:
I use the code below to study delete and destructor.
#include <iostream>
using namespace std;
struct A {
virtual ~A() { cout << "~A()" << endl; }; //LINE1
void operator delete(void* p) {...
|
by: rohits123 |
last post by:
I have an overload delete operator as below
//////////////////////////////////
void operator delete(void* mem,int head_type) {
mmHead local_Head = CPRMemory::GetMemoryHead(head_type);...
|
by: mdinino |
last post by:
Hi,
I am new to C++, and I have a simple question but I can't seem to find
a direct answer.
If I use new to create something in the scope of a function and then I
return the pointer that I...
|
by: =?iso-8859-1?q?Tom=E1s_=D3_h=C9ilidhe?= |
last post by:
"delete" does two things:
1) Invokes the destructor
2) Deallocates the memory
We can manually invoke the destructor with:
p->~T();
But is there any way to manually deallocate the memory...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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: 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,...
|
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...
|
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,...
| |