[cfe-dev] Dynamic memory allocation and deleted definitions

David Blaikie dblaikie at gmail.com
Wed Nov 13 10:19:53 PST 2013


On Wed, Nov 13, 2013 at 10:10 AM, Rahul Jain <1989.rahuljain at gmail.com>wrote:

>
> Hi all,
>
> clang version 3.4 (194264)
> Target: i386-pc-linux-gnu
> Thread model: posix
>
> gcc version 4.8.1
>
> This is with respect to the following test code:
>
> struct A
> {
>   ~A() = delete;
> };
>
>
> int main()
> {
>   A* ap = new A[5];
> }
>
> While g++ compiles it without any errors , clang throws an error:
>
> test.cpp:9:11: error: attempt to use a deleted function
>   A* ap = new A[5];
>           ^
> test.cpp:3:3: note: function has been explicitly marked deleted here
>   ~A() = delete;
>   ^
> 1 error generated.
>
> My question is:
>
> How does the destructor come into picture when we are not explicitly
> deleting the memory allocated?
>
> The standard says: A program that refers to a deleted function implicitly
> or explicitly, other than to declare it, is ill-formed.
>
> But I suppose the destructor never gets called in this case. So ideally
> this test case should compile without any errors. Please correct me if my
> understanding is incorrect.
>
>
> Interestingly the error is not thrown when I tweak the test case to
> allocate a single instance instead of an array. Something like this:
>
> struct A
> {
>   ~A() = delete;
> };
>
>
> int main()
> {
>   A* ap = new A;
> }
>
> This piece of code compiles fine with both g++ and clang++ as expected.
>
> Is this a potential bug in clang or am I missing something obvious??
>

I'd have to check wording to see the finer points, but certainly the issue
you are hitting is that, if A's ctor throws on one of the constructed
objects in the array, the dtor for the previous array elements needs to be
run.

I'm not sure if the language requires that this check only occur for
non-nothrow ctors or not.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20131113/64a77eb8/attachment.html>


More information about the cfe-dev mailing list