Re: c++ delete operator parameters



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
.



Relevant Pages

  • Re: Cartoons cause attacks, threats against EU, Denmark, Norway
    ... Recklessly refusing to invoke the Fifth Amendment, ... Someday I'll find out what I'm not invoking here. ... It's a reference to the Fifth Amendment to the United States ... Germany and the Holocaust for the Inquisition. ...
    (rec.arts.comics.strips)
  • Re: Multithreading and updating the GUI
    ... Your form delegate method has more implicit information and can more appropriately manage the decision as to whether to call Invoke() or not. ... Unless your worker thread is specifically designed to deal with GUI classes, I think it's better to leave the invoking to the actual GUI code. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Advise on Real-Time update to column control
    ... I think the problem should be the multithreading ... That is in .Net Framework, when using multi-thread, ... This is the multithreading invoking rule ... invoke, then use Control.BeginInvoke method to invoke this delegate. ...
    (microsoft.public.dotnet.framework.windowsforms)
  • Re: invoking
    ... Make that "invoking a proc by space key, ... However, it won't invoke either button if focus is on X and Y, but it will invoke either button if focus is on Z". ... set class ...
    (comp.lang.tcl)
  • Re: Microfocus Netexpress question : COBSQL vs openESQL
    ... > not yet solved to our liking. ... Occasionally code would not execute though ... compile using the Cobsql preprocessor (which would invoke Pro*COBOL under ...
    (comp.lang.cobol)

Loading