By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,182 Members | 1,364 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,182 IT Pros & Developers. It's quick & easy.

Corresponding forms of new and delete

P: n/a

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
Share this Question
Share on Google+
2 Replies


P: n/a

"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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.