473,511 Members | 16,258 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Corresponding forms of new and delete


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
delete with a signature that corresponds to that of the "new" being used
will be called. However, during a "normal" deallocation, there is no way to
know which version of "new" was previously used, so the usual deallocation
routine is used (I hope I'm using the term "usual" properly here). This is
demonstrated in the output shown above.

When the statement "delete ptr;" is reached, is there a way to cause it to
use "operator delete(void*, int)" as the underlying deallocation routine
rather than "operator delete(void *)"? After all, the usual dellaocation
routine may not do what's right given that the memory was not allocated with
the usual allocation routine! (Again, I hope I'm using the term "usual"
properly.)

I am aware that I could do the following in place of "delete ptr;":

ptr->~base();
base::operator delete(ptr, 243);

However, the point of this article is to see if there is a way to do what I
desire without using explicit calls to the destructor or "operator delete".

Thanks,
Dave
#include <iostream>

using namespace std;

struct base
{
base()
{
cout << "base::base()" << endl;
}

~base()
{
cout << "base::~base()" << endl;
}

void *operator new(size_t size)
{
cout << "base::operator new(size_t)" << endl;
return ::operator new(size);
}

void *operator new(size_t size, int)
{
cout << "base::operator new(size_t, int)" << endl;
return ::operator new(size);
}

void operator delete(void *ptr)
{
cout << "base::operator delete(void *)" << endl;
::operator delete(ptr);
}

void operator delete(void *ptr, int)
{
cout << "base::operator delete(void *, int)" << endl;
::operator delete(ptr);
}
};

int main()
{
base *ptr(new(243) base);

delete ptr;

return 0;
}


Jul 19 '05 #1
2 2568

"Dave" <be***********@yahoo.com> wrote
In the case of an exception being thrown during construction, a form of
delete with a signature that corresponds to that of the "new" being used
will be called. However, during a "normal" deallocation, there is no way to
know which version of "new" was previously used, so the usual deallocation
routine is used (I hope I'm using the term "usual" properly here). This is
demonstrated in the output shown above.

When the statement "delete ptr;" is reached, is there a way to cause it to
use "operator delete(void*, int)" as the underlying deallocation routine
rather than "operator delete(void *)"? After all, the usual dellaocation
routine may not do what's right given that the memory was not allocated with
the usual allocation routine! (Again, I hope I'm using the term "usual"
properly.)


You could allocate a few extra bytes in both the allocation functions to store
a flag, then check the flag in the deallocation functions and redirect as
appropriate. Alternatively, keep an associative array which records which
the addresses of any active blocks allocated by "operator new (void *, int)"
and call "operator delete (void *, int)" from "operator delete (void *)" when a
matching pointer is passed in.

That might not be what you want. Just an idea.

Good luck,
Buster.
Jul 19 '05 #2
On Sun, 19 Oct 2003 20:01:33 -0700, "Dave" <be***********@yahoo.com>
wrote:

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
delete with a signature that corresponds to that of the "new" being used
will be called. However, during a "normal" deallocation, there is no way to
know which version of "new" was previously used, so the usual deallocation
routine is used (I hope I'm using the term "usual" properly here). This is
demonstrated in the output shown above.
You need to write the usual deallocation function to know what to do
with allocations from any of the normal allocation functions.
When the statement "delete ptr;" is reached, is there a way to cause it to
use "operator delete(void*, int)" as the underlying deallocation routine
rather than "operator delete(void *)"?
No. The correct function to call is only known at runtime, so you'll
need to track it yourself.

After all, the usual dellaocationroutine may not do what's right given that the memory was not allocated with
the usual allocation routine! (Again, I hope I'm using the term "usual"
properly.)
You'll have to write it to do the right thing.

I am aware that I could do the following in place of "delete ptr;":

ptr->~base();
base::operator delete(ptr, 243);

However, the point of this article is to see if there is a way to do what I
desire without using explicit calls to the destructor or "operator delete".


I've modified the code to call the right operator delete.

Tom
#include <iostream>

using namespace std;

struct base
{
base()
{
cout << "base::base()" << endl;
}

~base()
{
cout << "base::~base()" << endl;
}

void *operator new(size_t size)
{
cout << "base::operator new(size_t)" << endl;
unsigned* ptr = static_cast<unsigned*>(::operator new(size +
sizeof(unsigned)));
*ptr = 0;
return ptr + 1;
}

void *operator new(size_t size, int)
{
cout << "base::operator new(size_t, int)" << endl;
unsigned* ptr = static_cast<unsigned*>(::operator new(size +
sizeof(unsigned)));
*ptr = 1;
return ptr + 1;
}

void operator delete(void *ptr)
{
unsigned* uptr = static_cast<unsigned*>(ptr);
if (uptr[-1] == 1u)
{
cout << "base::operator delete(void *, int)" << endl;
}
else
{
cout << "base::operator delete(void *)" << endl;
}

::operator delete(uptr - 1);
}

void operator delete(void *ptr, int)
{
cout << "base::operator delete(void *, int)" << endl;
unsigned* uptr = static_cast<unsigned*>(ptr);
::operator delete(uptr - 1);
}
};

int main()
{
base *ptr(new(243) base);

delete ptr;

return 0;
}
Jul 19 '05 #3

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

Similar topics

1
2082
by: Dodo | last post by:
I have overloaded the global new/delete operators with something like this (simplified): void *operator new(size_t size) { ...allocation code... } void operator delete(void * p) {
3
672
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...
3
5108
by: François | last post by:
Hello, I'm currentry porting a vba project into vb .net Things are going smoothly for the moment, but I'm loosing a lot of time to find the counterparts constants name for ex :...
11
5019
by: Peter Olcott | last post by:
I have just built a class that provides the most useful subset of std::vector functionality for use by compilers that lack template capability. http://home.att.net/~olcott/std_vect.html ...
2
9261
by: Chris Bolus | last post by:
I'm a teacher using MS Access on an RMConnect 2.4 network. On some workstations both I and my students sometimes get an error message when attempting to insert a command button on a form which...
3
6638
by: Richard Hollenbeck | last post by:
I have the following query in my form's code: Private Function Get_Data(fieldNum As Integer) Dim strSQL As String Dim db As DAO.Database Dim rs As DAO.Recordset strSQL = "SELECT & "", "" & ...
6
1302
by: al | last post by:
Greetings, Thanks in advance for your kind attension. I have been searching for documents about dynamically coding MDI forms when working with datasets. Does anyone know of any??? MTIA,...
2
1341
by: najeebasajid | last post by:
hi guys, I'm new in asp, and hav a question. I have three forms named, read, delete and compose on a single page, it3.asp as ..... <html> <head> <title>Inbox</title> <script...
0
1357
by: hennas | last post by:
Basically i want to design a membership Name and Telephone List form using the following command buttons. Edit Add New; Update; Delete; Cancel; Save; Clear, and Exit
0
7242
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,...
1
7075
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...
0
7508
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...
0
5662
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,...
1
5063
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...
0
3222
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...
0
3212
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1572
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 ...
1
781
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.