<div dir="ltr"><br><div class="gmail_extra"><br><br><div class="gmail_quote">On Wed, Nov 13, 2013 at 10:39 AM, Rahul Jain <span dir="ltr"><<a href="mailto:1989.rahuljain@gmail.com" target="_blank">1989.rahuljain@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">But why will the destructor get called if the array is not explicitly deleted?<div><br></div><div>Consider the following piece of code without the deleted definition.</div>
<div><br></div><div><div>#include<stdio.h></div>
<div><br></div><div>struct A</div><div>{</div><div>  ~A() {</div><div>  printf ("Inside destructor\n");</div><div class="im"><div>  }</div><div>};</div><div><br></div><div><br></div><div>int main()</div><div>{</div>
<div>  A* ap = new A[5];</div>
</div><div>  delete []ap;</div><div>}</div></div><div><br></div><div>On executing this "Inside destructor" gets printed 5 times.</div><div><br></div><div>But if we remove the delete statement, nothing gets printed i.e the destructor is never run. </div>

<div><br></div><div>I did not get what you meant by saying "<span style="font-family:arial,sans-serif;font-size:13px">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</span>"? </div>
</div></blockquote><div><br>Are you familiar with C++ exceptions?<br><br>Try something like this:<br><br>int i = 0;<br><br>struct A {<br>  A() { if (++i == 2) { throw 0; } }<br>  ~A() { printf("Inside destructor\n"); }<br>
};<br><br>int main() {<br>  A *ap = new A[2];<br>}<br><br>Even without the delete statement, you should see "Inside destructor" print once.<br><br>As I said, it's possible that if the constructor is 'nothrow' then Clang is meant to not need the dtor here, I haven't checked the exact wording of the standard.</div>
</div></div></div>