{"id":4134,"date":"2014-03-30T08:06:09","date_gmt":"2014-03-30T08:06:09","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/can-someone-explain-exactly-what-happens-if-an-exception-is-thrown-during-the-process-of-allocating-an-array-of-objects-on-the-heap-collection-of-common-programming-errors\/"},"modified":"2014-03-30T08:06:09","modified_gmt":"2014-03-30T08:06:09","slug":"can-someone-explain-exactly-what-happens-if-an-exception-is-thrown-during-the-process-of-allocating-an-array-of-objects-on-the-heap-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/can-someone-explain-exactly-what-happens-if-an-exception-is-thrown-during-the-process-of-allocating-an-array-of-objects-on-the-heap-collection-of-common-programming-errors\/","title":{"rendered":"Can someone explain exactly what happens if an exception is thrown during the process of allocating an array of objects on the heap?-Collection of common programming errors"},"content":{"rendered":"<p>The &#8220;atomic&#8221; C++ allocation and construction functions <em>are<\/em> correct and exception-safe: If <code>new T<\/code>; throws, nothing leaks, and if <code>new T[N]<\/code> throws anywhere along the way, everything that&#8217;s already been constructed is destroyed. So nothing to worry there.<\/p>\n<p>Now a digression:<\/p>\n<p>What you <strong>always<\/strong> must worry about is using <em>more than one<\/em> <code>new<\/code> expression in any single unit of responsibility. Basically, you have to consider any <code>new<\/code> expression as a hot potato that needs to be absorbed by a fully-constructed, responsible guardian object.<\/p>\n<p>Consider <code>new<\/code> and <code>new[]<\/code> strictly as library building blocks: You will never use them in high-level user code (perhaps with the exception of a single <code>new<\/code> in a constructor), and only inside library classes.<\/p>\n<p>To wit:<\/p>\n<pre><code>\/\/ BAD:\nA * p = new A;\nB * q = new B;  \/\/ Ouch -- *p may leak if this throws!\n\n\/\/ Good:\nstd::unique_ptr p(new A);\nstd::unique_ptr<b> q(new B); \/\/ who cares if this throws\nstd::unique_ptr r(new C[3]); \/\/ ditto\n<\/b><\/code><\/pre>\n<p><b>As another aside: The standard library containers implement a similar behaviour: If you say <code>resize(N)<\/code> (growing), and an exception occurs during <em>any<\/em> of the constructions, then <em>all<\/em> of the already-constructed elements are destroyed. That is, <code>resize(N)<\/code> either grows the container to the specified size or not at all. <sub>(E.g. in GCC 4.6, see the implementation of <code>_M_fill_insert()<\/code> in <code>bits\/vector.tcc<\/code> for a library version of exception-checked range construction.)<\/sub><\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The &#8220;atomic&#8221; C++ allocation and construction functions are correct and exception-safe: If new T; throws, nothing leaks, and if new T[N] throws anywhere along the way, everything that&#8217;s already been constructed is destroyed. So nothing to worry there. Now a digression: What you always must worry about is using more than one new expression in [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4134","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4134","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4134"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4134\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4134"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4134"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4134"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}