473,412 Members | 2,048 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,412 software developers and data experts.

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 1614
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
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...
1
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...
3
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...
1
by: Douglas Peterson | last post by:
class Allocator { public: virtual void * Alloc(size_t) = 0; virtual void * Free(void*) = 0; }; class Object { public:
2
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...
3
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 >>...
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);...
10
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...
15
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 -...
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
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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,...
0
isladogs
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...

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.