Re: Delete without calling destructor?
On Feb 1, 10:28 pm, "Alf P. Steinbach" <al...@start.no> wrote:
* Tom=E1s =D3 h=C9ilidhe:
SomeSortOfPlacementDelete(p); /* Just de-allocate, don't destruct */=
If you know the exact size used for the original allocation
you could perhaps use ::operator delete.
Why would you need to know the size?
What you do need to know is the actual address returned by
the ::operator new function. If there's no inheritance
involved, this is the address of the object, of course, but if
all you've got is a pointer to base, then it might not be the
correct address. (But the only cases I can think of where
calling ::operator delete() directly would make sense is when
you've also called ::operator new() directly. So just save that
address, and use it.)
However I recommend, instead, to not do what is most likely
premature optimization.
And anyway, if measurements show you really need to avoid
those "needless" allocations and deallocations, define
operator new and operator delete for the class, and/or use an
allocator more suitable for this particular situation than the
built-in one.
That's certainly a better solution. And I think boost has a
fixed length allocator, which means you don't even have to
implement that (not that it's very difficult, but using the
Boost code means writing 0 lines of code yourself, and it's very
difficult to do better).
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34