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;
}