472,354 Members | 1,642 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,354 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 2455

"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
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
1
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. header("Location:".$urlback); Is this the right layout the...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it so the python app could use a http request to get...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...

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.