{"id":4748,"date":"2014-03-30T15:05:37","date_gmt":"2014-03-30T15:05:37","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/how-is-array-memory-allocated-and-freed-in-c-and-c-collection-of-common-programming-errors\/"},"modified":"2014-03-30T15:05:37","modified_gmt":"2014-03-30T15:05:37","slug":"how-is-array-memory-allocated-and-freed-in-c-and-c-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/how-is-array-memory-allocated-and-freed-in-c-and-c-collection-of-common-programming-errors\/","title":{"rendered":"How is *array* memory allocated and freed in C and C++?-Collection of common programming errors"},"content":{"rendered":"<p>My question specifically is in regards to arrays, <strong>not<\/strong> objects.<\/p>\n<p>There a few questions on SO about <code>malloc()<\/code>\/<code>free()<\/code> versus <code>new<\/code>\/<code>delete<\/code>, but all of them focus on the differences in how they are used. I understand how they are used, but I don&#8217;t understand what underlying differences cause the differences in usage.<\/p>\n<p>I often hear C programmers say that <code>malloc()<\/code> and <code>free()<\/code> are costly operations, but I&#8217;ve never heard a C++ programmer say this about <code>new<\/code> and <code>delete<\/code>. I&#8217;ve also noticed that C++ doesn&#8217;t have an operation that corresponds to C&#8217;s <code>realloc()<\/code>.<\/p>\n<p>If I were writing an equivalent to C++&#8217;s <code>vector<\/code> class, I would want to avoid copying the entire array when resizing it, but with <code>new<\/code> and <code>delete<\/code> you have to copy. In C, I would simply <code>realloc()<\/code>. It&#8217;s worth noting that <code>realloc()<\/code> might just copy the entire array, but my impression was that it used the same pointer and allocated less space for it, at least when sizing down.<\/p>\n<p>So my question is, how do the algorithms used by <code>malloc()<\/code> and <code>free()<\/code> differ from those used by <code>new<\/code> and <code>delete<\/code>. More specifically, why does the C way have a stigma of being more expensive, and why doesn&#8217;t the C++ way allow resizing without copying?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>My question specifically is in regards to arrays, not objects. There a few questions on SO about malloc()\/free() versus new\/delete, but all of them focus on the differences in how they are used. I understand how they are used, but I don&#8217;t understand what underlying differences cause the differences in usage. I often hear C [&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-4748","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4748","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=4748"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4748\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4748"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4748"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4748"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}