I want to add a new operator for one of my classes, that looks like this:

void* operator new(size_t Size, void* tag);

What should look like the delete operator?

If I use something like this:
void operator delete(void* Buffer, void* tag)

it will not be called when I "delete" a class instance.

If I use a delete operator like this:

void operator delete(void* Buffer)

It will be called, but I will get a complie warning because it's not
matching my new operator.



So, what is the right delete operator?

Re: new and delete operators for a class.. by William

William
Thu Feb 24 14:49:03 CST 2005

"Gabriel Bogdan" <na@na.na> wrote in message
news:%23a9Nj2qGFHA.2620@tk2msftngp13.phx.gbl...
>I want to add a new operator for one of my classes, that looks like this:
>
> void* operator new(size_t Size, void* tag);
>
> What should look like the delete operator?

Does this not work?
void YourClass::operator delete(void *p)
{
// ...
}

Regards,
Will



Re: new and delete operators for a class.. by Gabriel

Gabriel
Thu Feb 24 15:08:33 CST 2005


"William DePalo [MVP VC++]" <willd.no.spam@mvps.org> wrote in message
news:ujZPHJrGFHA.580@TK2MSFTNGP15.phx.gbl...
> "Gabriel Bogdan" <na@na.na> wrote in message
> news:%23a9Nj2qGFHA.2620@tk2msftngp13.phx.gbl...
> >I want to add a new operator for one of my classes, that looks like this:
> >
> > void* operator new(size_t Size, void* tag);
> >
> > What should look like the delete operator?
>
> Does this not work?
> void YourClass::operator delete(void *p)
> {
> // ...
> }

As I sed, this form of delete operator does work, but I also see this at
compile time:
"c:\Proiecte\ClassesTest\ClassesTest.cpp(54) : warning C4291: 'void
*TmpB::operator new(size_t,void *)' : no matching operator delete found;
memory will not be freed if initialization throws an exception"



Re: new and delete operators for a class.. by William

William
Thu Feb 24 16:15:45 CST 2005

"Gabriel Bogdan" <na@na.na> wrote in message
news:e8aIkUrGFHA.3068@tk2msftngp13.phx.gbl...
> As I sed, this form of delete operator does work, but I also see this at
> compile time:
> "c:\Proiecte\ClassesTest\ClassesTest.cpp(54) : warning C4291: 'void
> *TmpB::operator new(size_t,void *)' : no matching operator delete found;
> memory will not be freed if initialization throws an exception"

You might want to take a look at this link

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclang/html/_pluslang_the_new_and_delete_operators.asp

and read about the interaction of exceptions and new operators.

(Sorry to ask but I don't save posts) Did you post the code for _all_ of the
new and delete operators defined in your class?

Regards,
Will



Re: new and delete operators for a class.. by Craig

Craig
Thu Feb 24 16:27:23 CST 2005

"Gabriel Bogdan" wrote:

> I want to add a new operator for one of my classes, that looks like
> this:
>
> void* operator new(size_t Size, void* tag);
>
> What should look like the delete operator?
>
> If I use something like this:
> void operator delete(void* Buffer, void* tag)
>
> it will not be called when I "delete" a class instance.
>
> If I use a delete operator like this:
>
> void operator delete(void* Buffer)
>
> It will be called, but I will get a complie warning because it's not
> matching my new operator.
>
> So, what is the right delete operator?

They both are. The regular delete operator is used for delete operations
*unless* the ctor called for your placement new throws an exception. So you
need both. Below's a little program that can show you what's going on.
Also, you might want to check out Meyers (I think it was Effective C++): he
discusses this at length. This is why I never use placement new (I just
never feel like I've got everything covered :)

Craig

//compiled with VC 7... cl /GX temp.cpp
#include <iostream>
#include <new>

using namespace std;

struct test {
test() { }

test(int i) {
cout << "test int ctor" << endl;
cout << "ctor about to throw..." << endl;
throw "OOPS!";
}

void* operator new(size_t s) {
cout << "test new" << endl;
return malloc(s);
}

void* operator new(size_t s, void* v) {
cout << "test placement new" << endl;
return malloc(s);
}

void operator delete(void* p) {
cout << "test delete" << endl;
free(p);
}

void operator delete(void* p, void* v) {
cout << "test placement delete" << endl;
free(p);
}
};

int main(void) {
test* p;

cout << "REGULAR NEW" << endl;
p = new test;
delete p;

cout << "PLACEMENT NEW" << endl;
p = new(NULL) test;
delete p;

cout << "PLACEMENT NEW THROW" << endl;
try {
p = new(NULL) test(1);
delete p;
} catch(...) {
cout << "catch" << endl;
}

cout << "REGULAR NEW THROW" << endl;
try {
p = new test(1);
delete p;
} catch(...) {
cout << "catch" << endl;
}

return 0;
}




Re: new and delete operators for a class.. by Gabriel

Gabriel
Thu Feb 24 16:29:55 CST 2005


"William DePalo [MVP VC++]" <willd.no.spam@mvps.org> wrote in message
news:O6q0j5rGFHA.1996@TK2MSFTNGP12.phx.gbl...
> "Gabriel Bogdan" <na@na.na> wrote in message
> news:e8aIkUrGFHA.3068@tk2msftngp13.phx.gbl...
> > As I sed, this form of delete operator does work, but I also see this at
> > compile time:
> > "c:\Proiecte\ClassesTest\ClassesTest.cpp(54) : warning C4291: 'void
> > *TmpB::operator new(size_t,void *)' : no matching operator delete found;
> > memory will not be freed if initialization throws an exception"
>
> You might want to take a look at this link
>
>
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclang/html/_pluslang_the_new_and_delete_operators.asp
>
> and read about the interaction of exceptions and new operators.
>
> (Sorry to ask but I don't save posts) Did you post the code for _all_ of
the
> new and delete operators defined in your class?

They are just dummys right now, empty functions.
The idea is that you need 2 types of delete operators (the documentation
dosn't really tell you this, as I read it).

void* operator new(size_t Size, void* tag)
{
return (TmpB*) new char[Size];
}

//the first one, this will only be called in case there is a exception in
the constructor
void operator delete(void* Buffer, void* tag)
{
}

//and the second one, this will be called when you delete the instance
void operator delete(void* Buffer)
{
}

Still, one more question, why if the delete operator has adi