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::operato r delete" << endl;
free(p);
}
};
struct B : A {
void operator delete(void* p) {
cout << "B::operato r 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 5 5522 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::operato r delete" << endl; free(p); } };
struct B : A { void operator delete(void* p) { cout << "B::operato r 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) ju******@gmail. com wrote: ... struct A { virtual ~A() { cout << "~A()" << endl; }; //LINE1
void operator delete(void* p) { cout << "A::operato r delete" << endl; free(p); } };
struct B : A { void operator delete(void* p) { cout << "B::operato r 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
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 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 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::operato r delete" << endl; free(p); } };
struct B : A { void operator delete(void* p) { cout << "B::operato r 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 virtual destructor. The class with the virtual
destructor has AddRef() and Release() methods, and Release() ultimately does
a "delete this". Can that "delete this" statement somehow find the right
delete method? If it does, it involves a downcast...
|
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;
|
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 will call it's destructor when it
is made a target of a delete".
|
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;
|
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>
| |
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 nice and working allocation deallocation
routines. However, I don't want to loose the nice extras of new operator,
like - constructor calling, typecasting the result, keeping the array size,
etc.
For another bunch of reasons, outside this scope I...
|
by: Douglas Peterson |
last post by:
class Allocator
{
public:
virtual void * Alloc(size_t) = 0;
virtual void * Free(void*) = 0;
};
class Object
{
public:
|
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) {
|
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 learn and refresh my memory? :-)
|
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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 captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |