{"id":4619,"date":"2014-03-30T14:01:02","date_gmt":"2014-03-30T14:01:02","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/what-is-the-difference-between-memory-allocation-through-new-and-allocator-collection-of-common-programming-errors\/"},"modified":"2014-03-30T14:01:02","modified_gmt":"2014-03-30T14:01:02","slug":"what-is-the-difference-between-memory-allocation-through-new-and-allocator-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/what-is-the-difference-between-memory-allocation-through-new-and-allocator-collection-of-common-programming-errors\/","title":{"rendered":"What is the difference between memory allocation through new and allocator-Collection of common programming errors"},"content":{"rendered":"<p>Eh, I think that <code>new<\/code> and <code>malloc<\/code> are different and <code>allocator<\/code> provides different functions. <code>malloc<\/code> returns non-initialized data, and <code>calloc<\/code> returns <em>zero<\/em>-ed data. But <code>new<\/code> would call the constructor if you are creating an instance of some class ( not <code>int<\/code>, <code>bool<\/code> these <em>primitive<\/em> types, which, by the way, can be initialized as well ). <code>delete<\/code> would call the destructor, while <code>free<\/code> doesn&#8217;t.<\/p>\n<p>As for <code>allocator<\/code>, it provides a layer of abstraction for the user. <code>allocator<\/code> can return constructed object, non-initialized memory space, or destroy a object or release the space. STL containers use <code>allocator<\/code> to get memory space and create object.<\/p>\n<p>But note that as custom <code>allocator<\/code> is possible, an <code>allocator<\/code> does not necessarily manage memory like <code>new<\/code>\/<code>delete<\/code>. It can create a large chunk of memory then do some <em>allocation cache<\/em>. It can return memory address in areas mapped to files on disk so that the internal data goes into the filesystem as it&#8217;s modified by the upper layer, <em>container<\/em>. Also it can call <code>new<\/code> to get memory. In this way, <code>allocator<\/code> enables user to build containers that lie in specific areas of the memory. So, with <code>allocator<\/code>s, the internal logic of containers are separated from the way memory storage is managed.<\/p>\n<p>Actually you can write a class derived from <code>std::allocator<\/code> to implement every feature mentioned above.<\/p>\n<p>you might want to read this for a more detailed discussion on <code>allocator<\/code>s.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Eh, I think that new and malloc are different and allocator provides different functions. malloc returns non-initialized data, and calloc returns zero-ed data. But new would call the constructor if you are creating an instance of some class ( not int, bool these primitive types, which, by the way, can be initialized as well ). [&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-4619","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4619","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=4619"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4619\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4619"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4619"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4619"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}