[cfe-dev] Dynamic memory allocation and deleted definitions

Rahul Jain 1989.rahuljain at gmail.com
Wed Nov 13 10:10:43 PST 2013


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??

Thanks,
Rahul
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20131113/40cefb1a/attachment.html>


More information about the cfe-dev mailing list