By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,889 Members | 1,358 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,889 IT Pros & Developers. It's quick & easy.

Interaction of delete and destructor

P: n/a
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;
free(p);
}
};

struct B : A {
void operator delete(void* p) {
cout << "B::operator 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::operator 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

Jun 22 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
ju******@gmail.com wrote:
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;
free(p);
}
};

struct B : A {
void operator delete(void* p) {
cout << "B::operator 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::operator 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
"::operator delete ()" to free the memory. Using 'free', as you did, is
invalid.

hth
--
jb

(reply address in rot13, unscramble first)
Jun 22 '06 #2

P: n/a
ju******@gmail.com wrote:
...
struct A {
virtual ~A() { cout << "~A()" << endl; }; //LINE1

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

struct B : A {
void operator delete(void* p) {
cout << "B::operator 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::operator 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::operator 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'?

--
Best regards,
Andrey Tarasevich
Jun 22 '06 #3

P: n/a
Thank you very much. I understand now.
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'?


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

Jun 22 '06 #4

P: n/a
ju******@gmail.com wrote:
> 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'?


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.

--
Best regards,
Andrey Tarasevich
Jun 22 '06 #5

P: n/a
ju******@gmail.com wrote:
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;
free(p);
}
};

struct B : A {
void operator delete(void* p) {
cout << "B::operator 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::operator 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
Jun 22 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.