473,545 Members | 1,759 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C++: destructor and delete

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 will call it's destructor when it
is made a target of a delete".

Does not make sense to me. Would not this result in an endless loop? (ie.
delete calls destructor and within destructor it calls delete, which in turn
calls the destructor again and so on....)

Any comments appreciated.

asasas

Jul 19 '05 #1
52 26956
Where are these statements from?
You have to differentiate between objects created dynamically on the heap
with the 'new' operator, and objects created on the stack in a scope.
Using the delete operator instructs the program to call the destructor of
the class and then free the memory for the object. Objects that simply go
out of scope implicitely have the same process performed on them.
"Newsnet Customer" <ni********@ipr imus.com.au> wrote in message
news:3f******** @news.iprimus.c om.au...
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 will call it's destructor when it is made a target of a delete".

Does not make sense to me. Would not this result in an endless loop? (ie.
delete calls destructor and within destructor it calls delete, which in turn calls the destructor again and so on....)

Any comments appreciated.

asasas

Jul 19 '05 #2

"Newsnet Customer" <ni********@ipr imus.com.au> wrote in message
news:3f******** @news.iprimus.c om.au...
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 will call it's destructor when it is made a target of a delete".

Does not make sense to me. Would not this result in an endless loop? (ie.
delete calls destructor and within destructor it calls delete, which in turn calls the destructor again and so on....)

Any comments appreciated.

asasas


Destructor does not call delete, so there is no loop.

john
Jul 19 '05 #3

"Newsnet Customer" <ni********@ipr imus.com.au> wrote in message
news:3f******** @news.iprimus.c om.au...
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.
Consider this code -

#include <iostream>
class A{
public:
A()
{
}
~A()
{
std::cout << "Inside D'tor";
}
};

void f()
{
A a;
A *ptrA = new A;
}

int main ()
{
f ();
}

If you mean that when f() returns two destructors will be called then it's
wrong.
Destructor would be called only for the object created on the heap.
In fact this is a memory leak.

Statement 2: "A dynamically created object will call it's destructor when it is made a target of a delete".

That's true.

Does not make sense to me. Would not this result in an endless loop? (ie.
delete calls destructor and within destructor it calls delete, which in turn calls the destructor again and so on....)

Destructor does not call delete unless in your destructor you write -
delete this;

HTH,
J.Schafer
Jul 19 '05 #4
Destructor would be called only for the object created on the heap.


I meant stack ofcourse ;-).
Jul 19 '05 #5
> "Newsnet Customer" <ni********@ipr imus.com.au> wrote in message
news:3f******** @news.iprimus.c om.au...
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 will call it's destructor when
it
is made a target of a delete".

Does not make sense to me. Would not this result in an endless loop?

(ie. delete calls destructor and within destructor it calls delete, which in

turn
calls the destructor again and so on....)

Any comments appreciated.

asasas


Destructor does not call delete, so there is no loop.

john


Generally speaking, you have a delete in a destructor method. That said, a
delete calls the destructor and the endless cycles continues. I'm sure there
is something im missing.
sasas
Jul 19 '05 #6

Where are these statements from?
You have to differentiate between objects created dynamically on the heap
with the 'new' operator, and objects created on the stack in a scope.
Using the delete operator instructs the program to call the destructor of
the class and then free the memory for the object.


and whats USUALLY in the destructor method? a delete statement also,and
hence the loop.
sasasasas
Jul 19 '05 #7
> > Destructor does not call delete, so there is no loop.
Generally speaking, you have a delete in a destructor method. That

said, a delete calls the destructor and the endless cycles continues. I'm sure there is something im missing.


A destructor never calls the delete operator on itself (this), hence no
endless loop. In a destructor the delete operator (if any) is only used
for other objects owned by that class instance, not for itself.

--
Peter van Merkerk
peter.van.merke rk(at)dse.nl


Jul 19 '05 #8
Newsnet Customer wrote:
Where are these statements from?
You have to differentiate between objects created dynamically on the heap
with the 'new' operator, and objects created on the stack in a scope.
Using the delete operator instructs the program to call the destructor of
the class and then free the memory for the object.

and whats USUALLY in the destructor method? a delete statement also,and
hence the loop.


What loop?

delete statements can often be found in destructors, but they
do not target the object whose destructor it is.

S

Jul 19 '05 #9


Destructor would be called only for the object created on the heap.


I meant stack ofcourse ;-).


incorrect. as indicated in statement 1. an object's (dynamically created
....meaning HEAP) destructor is called when the object goes out of scope.
Jul 19 '05 #10

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

Similar topics

6
15713
by: Alexander Stippler | last post by:
Hi, I have some question about the usage of delete. I have some reference counted classes, all derived from SharedObject. It's destructor looks like this: ~SharedObject() { if (--references_ == 0) { delete this;
11
10498
by: Stub | last post by:
Please answer my questions below - thanks! 1. Why "Derived constructor" is called but "Derived destructor" not in Case 1 since object B is new'ed from Derived class? 2. Why "Derived destructor" is called in Case 2 since only ~base() becomes "virtual" and ~Derived() is still non-virtual? 3. Does Case 3 show that we don't need any virtual...
14
2400
by: Timothy Madden | last post by:
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;
20
2710
by: frs | last post by:
For memory economization, I need to get rid if the virtual destructor. Under this constraint I get caught up in a design, where I need to call a destructor of a derived class from a base class. Briefly it could be displayed like the code below. It ends up deleting the member 'x' of 'B' twice. Why? Is there a way around? Thanks Frank
35
3276
by: Peter Oliphant | last post by:
I'm programming in VS C++.NET 2005 using cli:/pure syntax. In my code I have a class derived from Form that creates an instance of one of my custom classes via gcnew and stores the pointer in a member. However, I set a breakpoint at the destructor of this instance's class and it was never called!!! I can see how it might not get called at a...
7
1850
by: | last post by:
Hi, From 11.11 here, I know that member objects get their dtor's called autmatically: http://www.parashift.com/c++-faq-lite/dtors.html#faq-11.11 What if I have a pointer to a member object? e.g. (in C++)
5
5512
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) { cout << "A::operator delete" << endl;
23
2560
by: Ben Voigt | last post by:
I have a POD type with a private destructor. There are a whole hierarchy of derived POD types, all meant to be freed using a public member function Destroy in the base class. I get warning C4624. I read the description, decided that it's exactly what I want, and ignored the warning. Now I'm trying to inherit using a template. Instead of...
9
8152
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); mmFree(&local_Head,(char *)mem); CPRMemory::SetMemoryHeadAs(local_Head,head_type); } ///////////////////// void* operator new(size_t sz, int head_Type) {
0
7401
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...
0
7656
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. ...
0
7807
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...
1
7419
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5971
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5326
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4944
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...
1
1014
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
703
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.