473,664 Members | 2,773 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Q about delete

Hi all,

I am a little confused about the delete operator, so I have a
question. Suppose we have something like

class Base {
public:
void* operator new(std::size_t size);
void operator delete(void* ptr);

//The rest goes here.
}

class Derived : public Base {
public:
void* operator new(std::size_t size);
void operator delete(void* ptr);

//The rest goes here.
}
Derived* der = new Derived;

Base* base = der; //Implicit upcast.

delete base;
In this case, it is the static type of the pointer that matters, so it
is the Base delete operator that gets called. In fact, by the time
delete is called the object has already been "demoted" to void* -
there is no concept of dynamic type to speak of. Am I correct?

And if the answer is yes, how do I organize things such that the
Derived delete gets called?

The use case I have is of a single-root hierarchy of heap-allocated
objects handled via smart pointers. The root operator new and delete
is just a general-purpose allocator but each class can have (and it
often does) its own special allocators.

TIA, best regards,
G. Rodrigues
Jan 3 '07 #1
1 1625
Gonçalo Rodrigues wrote:
I am a little confused about the delete operator, so I have a
question. Suppose we have something like

class Base {
public:
void* operator new(std::size_t size);
void operator delete(void* ptr);

//The rest goes here.
}
You need at least a semicolon, a virtual destructor (see
http://www.parashift.com/c++-faq-lit...html#faq-20.7),
a "static" on each operator, and a size_t parameter to your delete
operator. However, as _C++ Coding Standards_ Item 46 notes, if you
provide any class-specific form of new and delete, you should provide
all of them (plain, in-place, and non-throwing) since they will be
hidden otherwise and since the STL uses the in-place operator
extensively. They can be forwarding functions to the global versions
or, if the base class implements them, "using" declarations.
class Derived : public Base {
public:
void* operator new(std::size_t size);
void operator delete(void* ptr);

//The rest goes here.
}
You need a semicolon, "static" on each operator, and a size_t parameter
to your destructor, but more likely you want:

using Base::operator new;
using Base::operator delete;
Derived* der = new Derived;

Base* base = der; //Implicit upcast.

delete base;
In this case, it is the static type of the pointer that matters, so it
is the Base delete operator that gets called. In fact, by the time
delete is called the object has already been "demoted" to void* -
there is no concept of dynamic type to speak of. Am I correct?
Base's will be called, but the size_t parameter to the destructor will
be correct as long as you have a virtual destructor in your base class.
Stroustrup deals with this very example in section 15.6 of _TC++PL_ 3rd
ed.
And if the answer is yes, how do I organize things such that the
Derived delete gets called?
Delete a Derived pointer, or preferably, use the size_t parameter to
enable the Base class's delete operator to handle freeing any block
that is passed to it (the operator isn't calling the destructor after
all; it's just freeing the memory after the object has already been
destructed) and use the base class's operators in the derived class.

You might also be interested in this FAQ, which deals with memory
pooling:

http://www.parashift.com/c++-faq-lit...html#faq-11.14

Cheers! --M

Jan 3 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2587
by: Dave | last post by:
Hello all, In the code below, I see the following output: base::operator new(size_t, int) base::base() base::~base() base::operator delete(void *) In the case of an exception being thrown during construction, a form of
1
3844
by: Nimmi Srivastav | last post by:
There's a rather nondescript book called "Using Borland C++" by Lee and Mark Atkinson (Que Corporation) which presents an excellent discussion of overloaded new and delete operators. In fact there are quite a few things that I learned that I did not know before. For example, while I knew that the new and delete operators can be overloaded for classes, I did not know that that the global new and delete operators can also be overloaded. ...
3
9398
by: Nimmi Srivastav | last post by:
There's a rather nondescript book called "Using Borland C++" by Lee and Mark Atkinson (Que Corporation) which presents an excellent discussion of overloaded new and delete operators. I am presenting below a summary of what I have gathered. I would appreciate if someone could point out to something that is specific to Borland C++ and is not supported by the ANSI standard. I am also concerned that some of the information may be outdated...
1
3884
by: Douglas Peterson | last post by:
class Allocator { public: virtual void * Alloc(size_t) = 0; virtual void * Free(void*) = 0; }; class Object { public:
2
2081
by: Dave | last post by:
Hello all, I'd like to find a source on the web that discusses, in a comprehensive manner and in one place, everything about new / delete. It should include overloading operator new, the new operator, placement, nothrow, arrays, etc... My books cover the topic, I've found FAQs on the web that cover the topic, and so on, but all the sources I've found are disjointed. There's a bit on this page, a bit on that page, and so on. The...
3
4643
by: silver360 | last post by:
Hello, I'm trying to create a basic Heap manager and i have some question about new/delete overloading. The following code give me this output : >> $./heap >> registered : 0x804d098 >> 0x804d008 _Delete unknown block >> registered : 0x804d138 >> 0x804d008 _Delete unknown block >> 0x804d098 _Delete ok
9
8163
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) {
10
2085
by: jeffjohnson_alpha | last post by:
We all know that a new-expression, foo* a = new foo() ; allocates memory for a single foo then calls foo::foo(). And we know that void* p = ::operator new(sizeof(foo)) ; allocates a sizeof(foo)-sized buffer but does NOT call foo::foo().
15
5013
by: LuB | last post by:
I am constantly creating and destroying a singular object used within a class I wrote. To save a bit of time, I am considering using 'placement new'. I guess we could also debate this decision - but for the sake of this post ... I'm searching for an answer that assumes said decision. If I allocate memory in the class declaration: char buffer;
29
2252
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? :-)
0
8348
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,...
0
8863
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8779
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 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...
0
8636
tracyyun
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...
0
7376
agi2029
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...
0
5660
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4356
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2004
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1761
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.