C++: destructor and delete

  • Thread starter Newsnet Customer
  • Start date
N

Newsnet Customer

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
 
A

Andrew Ward

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.
 
J

John Harrison

Newsnet Customer said:
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
 
J

Josephine Schafer

Newsnet Customer said:
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
 
N

Newsnet Customer

Newsnet Customer said:
when

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
 
P

Peter van Merkerk

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.
 
N

Newsnet Customer

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
 
S

Scott Condit

Newsnet said:
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
 
N

Newsnet Customer

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.
 
J

Josephine Schafer

Newsnet Customer said:
incorrect. as indicated in statement 1. an object's (dynamically created
...meaning HEAP) destructor is called when the object goes out of scope.

Why don't you run the code I posted and see for yourself if the destructor
gets called.
 
K

Karl Heinz Buchegger

Newsnet said:
Hi,

Statement 1: "A dynamically created local object will call it's destructor
method when it goes out of scope when a procedure returms"

A dynamically created local object ...

int main()
{
std::string *p;

p = new std::string;

.... calls it's destructor method when it goes out of scope

well. p goes out of scope. But that does not mean, that the
dynamically created object, where p points to is destroyed.
Agree.

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

p = new std::string;

...

delete p;

the string which is pointed to by p is destroyed.
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....)

Why should this happen?
 
J

John Harrison

Newsnet Customer said:
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.

Sometimes you delete a subobject in the destructor. You don't delete the
original object.

class X
{
~X()
{
delete ptr; // delete Y object, calls Y destructor
}
Y* ptr;
};

X* p = new X;
delete p; // deletes an X object, calls X destructor

The only way there would be a loop was if you did 'delete this' in the
destructor.

john
 
J

John Harrison

Newsnet Customer said:
incorrect. as indicated in statement 1. an object's (dynamically created
...meaning HEAP) destructor is called when the object goes out of scope.

No it is not.

{
X* ptr = new X;
}

No destructors are called in the above code, because POINTERS do not have
destructors. ptr is a POINTER to X, its is not an X.

{
X x;
}

X destructor is called (assuming it has one).

You're very cocky considering, by your own admission, you don't get it.

john
 
K

Karl Heinz Buchegger

Newsnet said:
incorrect. as indicated in statement 1. an object's (dynamically created
...meaning HEAP) destructor is called when the object goes out of scope.

The point is: a dynamically created object(that means on the heap) *never*
goes out of scope. The object is destroyed when you explicitely say so
with the help of delete.

{
std::string* p;
p = new std::string;
}

At this point, p goes out of scope and hence is destroyed.
But the dynamically allocated string object lives on, even
if there is no means to access it any longer.
 
N

Noah Roberts

Newsnet said:
incorrect. as indicated in statement 1. an object's (dynamically created
...meaning HEAP) destructor is called when the object goes out of scope.

You misunderstand the definition of stack and heap. The word "stack"
used in this context is usually speaking of the stack frame. The stack
frame is a segment of memory, which also happens to be part of a larger
stack, which is used to contain return information as well as local
variables. When the function returns (or scope is lost on these stack
items) then the system esentially calls "delete" on all local variables.

The heap is just the main memory block. You request segments of this
heap be allocated to your program by using calls such as new or malloc.
You must give this memory back to the system when you are done.

There is very little difference in the actual objects contained within
the stack and heap. They all look the same. The only difference is
that stack objects are returned by the process automagically whereas
heap items must be returned by the programmer. Another difference is
that you refer to heap items based on a variable contained in the stack
- these are pointers.

If, as you say, there was an endless loop in calling delete on a heap
item then it would also occur in the stack since both methods call the
destructor. If the destructor deletes the object being deleted then
things blow up no matter where in memory that object is.

NR
 
K

Kevin Goodsell

Newsnet said:
incorrect. as indicated in statement 1. an object's (dynamically created
...meaning HEAP) destructor is called when the object goes out of scope.

Your first statement was incorrect. Josephine is correct.

-Kevin
 
N

Newsnet Customer

Sometimes you delete a subobject in the destructor. You don't delete the
original object.

class X
{
~X()
{
delete ptr; // delete Y object, calls Y destructor
}
Y* ptr;
};

X* p = new X;
delete p; // deletes an X object, calls X destructor

The only way there would be a loop was if you did 'delete this' in the
destructor.

john

If you perform a delete on a object, which then calls it's destructor
method, and this method is simply empty. How is the original object actually
being deleted? I can only assume the delete operator calls the destructor
method and then deletes the original object (outside of the destructor),
which you don't explicitly do yourself.

sasas
 
N

Newsnet Customer

A dynamically created local object ...

int main()
{
std::string *p;

p = new std::string;

... calls it's destructor method when it goes out of scope

well. p goes out of scope. But that does not mean, that the
dynamically created object, where p points to is destroyed.


Right. That means the destructor method only performs "house keeping" and
memory management of other objects/subjects and never memory mamagement on
itself (this).

p = new std::string;

...

delete p;

the string which is pointed to by p is destroyed.


Why should this happen?

Well it shouldn't now that i know that the destuctor never calls delete on
itself (this).

Thanks, you cleared up my misunderstandings.


assasas
 
N

Newsnet Customer

No it is not.

{
X* ptr = new X;
}

No destructors are called in the above code, because POINTERS do not have
destructors. ptr is a POINTER to X, its is not an X.

The delete operator is only performed on pointers, so it calls the
destructor method of the object it's pointing to. In your case, the
destructor of X is called when the procedure returns.

{
X x;
}

X destructor is called (assuming it has one).

An object will always have a constructor and a destructor. If either is not
defined, they will be created for you implicitly. Since pointers are objects
too, they will have a constructor and destructor.
You're very cocky considering, by your own admission, you don't get it.

john

I do get statement 1 of my original post, but I did not get statement 1.
Does not mean I have no authority to speak on the matter regarding statement
1? I don't think so.


sasasas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top