Interaction of delete and destructor

J

junw2000

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::eek:perator delete" << endl;
free(p);
}
};

struct B : A {
void operator delete(void* p) {
cout << "B::eek:perator delete" << endl; //LINE2
free(p);
}
~B(){ cout << "~B()" << endl; }; //LINE3
};

int main() {
A* ap ;
B *bp = new B;
ap = bp;
delete ap; //LINE4
}

The output of the code is:
~B()
~A()
B::eek:perator delete

I can not understand the output. When LINE4 is executed, LINE1 is
executed, right?
Since ~A() is virtual, LINE3 is called. How can LINE2 be executed?
"delete ap" calls A's destructor. Then B's destructor is called. Can
B's destructor call B's operator delete?

In general, delete invokes destructor, right? Can destructor invoke
delete?

Thanks a lot. I can not find the answer from my C++ books.

Jack
 
J

Jakob Bieling

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::eek:perator delete" << endl;
free(p);
}
};

struct B : A {
void operator delete(void* p) {
cout << "B::eek:perator delete" << endl; //LINE2
free(p);
}
~B(){ cout << "~B()" << endl; }; //LINE3
};

int main() {
A* ap ;
B *bp = new B;
ap = bp;
delete ap; //LINE4
}

The output of the code is:
~B()
~A()
B::eek:perator delete

I can not understand the output. When LINE4 is executed, LINE1 is
executed, right?
Since ~A() is virtual, LINE3 is called. How can LINE2 be executed?
"delete ap" calls A's destructor. Then B's destructor is called. Can
B's destructor call B's operator delete?

"delete ap" does not just call something. It is called a
delete-expression and it first destroys your object (by calling the
appropriate destructor(s)) and freeing the memory. Since you have
specified a custom function for the "free memory" part, this one will be
called instead of the built-in part.

Note, you also need to specify 'operator new' for your class, or use
"::eek:perator delete ()" to free the memory. Using 'free', as you did, is
invalid.

hth
 
A

Andrey Tarasevich

...
struct A {
virtual ~A() { cout << "~A()" << endl; }; //LINE1

void operator delete(void* p) {
cout << "A::eek:perator delete" << endl;
free(p);
}
};

struct B : A {
void operator delete(void* p) {
cout << "B::eek:perator delete" << endl; //LINE2
free(p);
}
~B(){ cout << "~B()" << endl; }; //LINE3
};

int main() {
A* ap ;
B *bp = new B;
ap = bp;
delete ap; //LINE4
}

The output of the code is:
~B()
~A()
B::eek:perator delete

I can not understand the output. When LINE4 is executed, LINE1 is
executed, right?

No. Delete-expression at LINE4 will first invoke the proper destructor
of the object. And that's 'B::~B()', i.e. LINE3.
Since ~A() is virtual, LINE3 is called.

Yes. As I said above 'B::~B()' is called. 'B::~B()' will in turn call
its parent's destructor 'A::~A()', LINE1. That's what you see in your
output.
How can LINE2 be executed?

This is a requirement of C++ language specification. 'operator delete'
is chosen as if the lookup for the operator is made from the proper
destructor of the object (by 'proper' I mean 'matching the dynamic type
of the object'). In this case the lookup for 'operator delete' is
performed from 'B::~B()'. 'operator delete' that is "seen" from
'B::~B()' is 'B::eek:perator delete', LINE2. That's why it is called.

How all this is achieved in practice belongs to the "compiler magic"
department.
"delete ap" calls A's destructor. Then B's destructor is called.

No, it's the other way around. Take a look at the output. 'delete ap'
calls B's destructor - 'B::~B()'. Then 'A::~A()' is called by 'B::~B()'.
Can B's destructor call B's operator delete?

Well, that how it is often implemented under the hood. But that's
nothing more that an implementation detail.
In general, delete invokes destructor, right? Can destructor invoke
delete?

What exactly do you mean by 'delete' in this case? Delete-expression or
'operator delete'?
 
J

junw2000

Thank you very much. I understand now.
What exactly do you mean by 'delete' in this case? Delete-expression or
'operator delete'?

Here I mean 'delete expression'. Now my understanding is that "delete
expression" invokes destructor; destructor can also invoke 'operator
delete' not 'delete expression'. Is my understanding correct?

C++ is really a complex language.:) How can I learn the "magic"
things? When writing code, is it necessary to know all these "magic"
things?

Thanks again.

Jack
 
A

Andrey Tarasevich

Here I mean 'delete expression'. Now my understanding is that "delete
expression" invokes destructor; destructor can also invoke 'operator
delete' not 'delete expression'. Is my understanding correct?

Not quite.

Delete-expression invokes destructor (or destructorS in case of '[]'
form) in order to "kill" the object(s). After that delete-expression
invokes 'operator delete' to release the memory.

Note, from the language point of view both destructor and
'operator-delete' are called by delete-expression. They don't call each
other.
 
M

Mark P

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::eek:perator delete" << endl;
free(p);
}
};

struct B : A {
void operator delete(void* p) {
cout << "B::eek:perator delete" << endl; //LINE2
free(p);
}
~B(){ cout << "~B()" << endl; }; //LINE3
};

int main() {
A* ap ;
B *bp = new B;
ap = bp;
delete ap; //LINE4
}

The output of the code is:
~B()
~A()
B::eek:perator delete

I can not understand the output. When LINE4 is executed, LINE1 is
executed, right?
Since ~A() is virtual, LINE3 is called. How can LINE2 be executed?
"delete ap" calls A's destructor. Then B's destructor is called. Can
B's destructor call B's operator delete?

In general, delete invokes destructor, right? Can destructor invoke
delete?

Thanks a lot. I can not find the answer from my C++ books.

Jack

Just a reminder that the "delete operator," the built-in operator that
you use when you write "delete [some_pointer];" is not the same as
"operator delete," the function that you may define within your class.
Conceptually you can think of the delete operator as doing two things:
invoking the destructor(s) of the object and then invoking operator
delete on the memory where the deconstructed object once was.

The distinction between the built-in "new operator" and a possibly
user-defined "operator new" is similar. When you write new [some_type],
thereby invoking the new operator, two things happen: operator new is
invoked which secures the memory for the object-to-be, and then the
constructor(s) is(are) invoked to actually construct the object.

Mark
 

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,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top