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

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 2555

"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
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
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
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
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
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
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
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
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...
0
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...

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.