Re: mix placement new with a standard delete
On Mar 24, 2:58 pm, Mike Kelley <mckelle...@gmail.com> wrote:
Is it legal to mix placement new with a standard delete operation?
I think so (compilers seem to agree), but should not be done. If you
overload new, you better have equivalent overload of delete. Consider:
class test
{
//...
test() { if (problem) throw some_exception; }
void* operator new(size_t, void* p) { return p; }
};
void* p = getmem();
auto_ptr<test> ptest(new (p) test);
If "problem" indeed happens, there is no matching operator delete of
the correct type to possibly free memory. For that, "test" needs
void operator delete(void* p, void*) { ::delete p; }
BTW, without that corresponding "delete", e.g. comeau warns:
Test::operator new(size_t, void *)" has
no corresponding operator delete (to be called if an
exception is
thrown during initialization of an allocated object)
Of course, you are required to match allocation/deallocation in your
new/delete overloads.
Goran.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]