[cfe-dev] Why the argument of `delete' is converted to void* ?

Enea Zaffanella zaffanella at cs.unipr.it
Wed Jul 4 10:13:37 PDT 2012


On 07/04/2012 05:07 PM, Jean-Daniel Dupas wrote:
>
> Le 4 juil. 2012 à 15:57, Enea Zaffanella a écrit :
[...]
>> Sorry for the misunderstanding, but the original question was not
>> really meant to be restricted to classes having a trivial destructor
>> (if you add a non-trivial destructor to class S then "delete ps" will
>> call it, whereas "operator delete(ps)" will not).
>>
>> To rephrase my question: since in the general case "delete ps" may
>> also call the destructor, why is its argument (always) converted to void*?
>>
>> Cheers,
>> Enea.
>
>
> AFAIK, even with a non trivial destructor, the code always end up
> calling the operator delete(void *) to free the memory.

Yes, the argument of "operator delete" has to be converted to a void*.

But this, afaict, is an inner implementation detail that should not be 
exposed in the AST (or it should be exposed elsewhere, e.g., in a method 
named getOperatorDeleteArg).

The argument of the CXXDeleteExpr is *not* subject to such an implicit 
conversion; as a matter of fact, the implicit conversion (if made 
explicit by the user) would change the semantics of the delete 
expression by killing the call of the destructor.

Enea.


> It will generate something like this:
>
> call the destructor on the struct.
> call operator delete((void *)ps);
>
> or in LLVM IR:
>    tail call void @_ZN1SD1Ev(%struct.S* %ps) nounwind // ps->~S()
>    %0 = getelementptr inbounds %struct.S* %ps, i64 0, i32 0
>    tail call void @_ZdlPv(i8* %0) nounwind // operator delete((void *)ps)
>    br label %delete.end
>
>
> -- Jean-Daniel
>
>
>
>





More information about the cfe-dev mailing list