473,408 Members | 1,702 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,408 software developers and data experts.

Interaction of delete and destructor

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
5 5496
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ian McBride | last post by:
(was: delete() confusion) I have a class with multiple base classes. One of these base classes (base1) has its own new/delete operators and nothing else. Another base class (base 2) has a...
6
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...
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...
14
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...
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:
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);...
29
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, I remembered delete is implemented through operator overloading, but I am not quite clear. Could anyone recommend some links about how delete is implemented so that I can...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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,...
0
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...
0
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...

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.