Re: c++ delete operator parameters
- From: "Doug Harrison [MVP]" <dsh@xxxxxxxx>
- Date: Wed, 07 Mar 2007 23:15:32 -0600
On Wed, 7 Mar 2007 20:56:31 -0800, Michael Crawley
<MichaelCrawley@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
If I can allocate memory on a custom memory pool like using 'new(heap)
TypeConstructor(),' why is it not allowed to deallocate by invoking
'delete(heap) valueReference' or 'delete[](valueReference, heap)?' I have to
invoke the destructor explicitly and invoke the static delete operator
explicitly for the type like 'var->~VarType(); VarType::operator delete(var,
heap).' However, after a quick Google, I have found others to suggest I am
mistaken, but I cannot get it to compile without some error. I know to have
overloaded 'delete' operators in order to properly cleanup memory if an
exception is thrown within a constructor after invoking a 'new' operator with
the same parameter list, but why can I not get to use them regularly?
While you can write "placement operator delete", it is used only when the
ctor in a matching placement new exits via an exception. If you want to
provide arguments to delete, you can just use a regular function. See
Stroustrup:
http://www.research.att.com/~bs/bs_faq2.html#placement-delete
Quiz: Why doesn't this apply to new?
I have also notice that you can code a completely bogus invocation to delete like
'delete(false, false, false, false, var),' but as long as the last argument
is a pointer, VS2003 will take and resolve it into assembly as an explicit
call to the destructor without calling any 'delete' operator. I have looked
at the assembly output files to validate this. Any advice is appreciated.
That compiles because you're using the comma operator, which evaluates to
the last expression in the list. I can't explain why the operator delete
function isn't being called; it is for this example:
void f()
{
delete (false, new char);
}
BTW, get rid of the parens; the "delete" keyword is not a function, it is
an operator (which is not to be confused with the function named "operator
delete") and doesn't need parens. Without the parens, the binding is
different, and the delete-expression won't compile, because "false" is not
a pointer.
--
Doug Harrison
Visual C++ MVP
.
- Prev by Date: Re: PNG loader crashing
- Next by Date: Re: alloca / _alloca / dynamic stack memory
- Previous by thread: Re: debug vs. release ?
- Next by thread: Re: c++ delete operator parameters
- Index(es):
Relevant Pages
|
Loading