[cfe-dev] Dynamic memory allocation and deleted definitions

Richard Smith richard at metafoo.co.uk
Wed Nov 27 13:07:03 PST 2013


On Wed, Nov 27, 2013 at 8:34 AM, Rahul Jain <1989.rahuljain at gmail.com>wrote:

> Thanks David for your valuable inputs.
>

David provided you with an approximation of the answer a few messages ago:

> 5.3.4p17 states "... If the new expression creates an array of objects of
class type, access and ambiguity control are done for the destructor"

This wording was somewhat defective, because it didn't deal with the case
of a deleted destructor (and also incorrectly suggests that a destructor
might be ambiguous). In the latest draft, this wording has been fixed, and
it now says:

5.3.4p19: "If the new-expression creates an array of objects of class type,
the destructor is potentially invoked (12.4)."
12.4p11: "A program is ill-formed if a destructor that is potentially
invoked is deleted or not accessible from the context of the invocation."

Hi Richard,
>
> Any comments on this behaviour??
>
> Thanks,
> Rahul
>
>
> On Wed, Nov 27, 2013 at 12:43 AM, David Blaikie <dblaikie at gmail.com>wrote:
>
>> On Tue, Nov 26, 2013 at 10:53 AM, Rahul Jain <1989.rahuljain at gmail.com>
>> wrote:
>> > I think I got something relevant: Do these words make any sense in our
>> > context?
>>
>> Not quite
>>
>> > An allocation or deallocation function
>>
>> Allocation/deallocation function does not refer to
>> constructor/destructor. See 3.4p1:
>>
>> "A C++ implementation provides access to, and management of, dynamic
>> storage via the global allocation functions operator new and operator
>> new[] and the global deallocation functions operator delete and
>> operator delete[]."
>>
>> These are actually not the ctor/dtor, in 3.4p2 their declarations are
>> shown as:
>>
>> void* operator new(std::size_t);
>> void* operator new[](std::size_t);
>> void operator delete(void*);
>> void operator delete[](void*);
>>
>> So, roughly, if you say "new T" first operator new(sizeof(T)) is
>> called, then the ctor for T is called on the returned storage, if the
>> T ctor throws, then operator delete is called on the storage before
>> the exception is propagated (that's why even non-array "new T"
>> odr-uses the deallocation function, operator delete - even though it
>> doesn't/shouldn't odr-use T's dtor).
>>
>> > for a class is odr-used by a
>> > new expression appearing in a potentially-evaluated expression as
>> specified
>> > in 5.3.4(New) and 12.5(Free store). A deallocation function for a class
>> is
>> > odr-used by a delete expression appearing in a potentially-evaluated
>> > expression as specified in 5.3.5(Delete) and 12.5(Free store).
>> >
>> > What exactly does one mean by saying that something is odr-used?
>>
>> The simplistic interpretation is that if something is odr-used it must
>> be defined (ie: there must be (one) definition). For example:
>>
>> void f1();
>> void f2();
>>
>> int main() {
>>   f1(); // odr use of f1 - the program is ill-formed if 'f1' is not
>> defined
>>   size_t s = sizeof(f2); // non-odr-use of f2 - the program is valid
>> even if 'f2' is never defined
>> }
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20131127/3dd208f2/attachment.html>


More information about the cfe-dev mailing list