473,387 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Can I delete without calling the destructor ?

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
Jul 22 '05 #1
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

Jul 22 '05 #2
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
Jul 22 '05 #3

"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 ?
Jul 22 '05 #4

"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 ...
Jul 22 '05 #5
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
Jul 22 '05 #6

"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;
}
}
Jul 22 '05 #7
> 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
Jul 22 '05 #8
>>
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
Jul 22 '05 #9
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

Jul 22 '05 #10
>>
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
Jul 22 '05 #11

"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

Jul 22 '05 #12

"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
Jul 22 '05 #13
"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.
Jul 22 '05 #14
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.
Jul 22 '05 #15

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

Similar topics

52
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...
20
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>
11
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...
1
by: Douglas Peterson | last post by:
class Allocator { public: virtual void * Alloc(size_t) = 0; virtual void * Free(void*) = 0; }; class Object { public:
10
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....
5
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) {...
9
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);...
6
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...
2
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...
0
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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$) { } ...
0
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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,...
0
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...
0
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,...

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.