{"id":4499,"date":"2014-03-30T12:09:27","date_gmt":"2014-03-30T12:09:27","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/c-array-of-derived-class-vs-array-of-pointers-of-base-class-to-derived-objects-why-is-amount-of-memory-allocated-so-much-different-collection-of-common-programming-errors\/"},"modified":"2014-03-30T12:09:27","modified_gmt":"2014-03-30T12:09:27","slug":"c-array-of-derived-class-vs-array-of-pointers-of-base-class-to-derived-objects-why-is-amount-of-memory-allocated-so-much-different-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/c-array-of-derived-class-vs-array-of-pointers-of-base-class-to-derived-objects-why-is-amount-of-memory-allocated-so-much-different-collection-of-common-programming-errors\/","title":{"rendered":"C++ array of derived class vs array of pointers of base class to derived objects &#8211; why is amount of memory allocated so much different?-Collection of common programming errors"},"content":{"rendered":"<p>I need some clarification on an issue I don&#8217;t quite understand. Using the two scenarios that follow, I would have thought that the amount of memory allocated would roughly be the same. However, scenario 2 gives me a <code>bad_alloc<\/code> exception after a while and appears to be chewing up memory like crazy (using Window&#8217;s Task Manager as a proxy for the amount of memory allocated to the process). The following is compiled on Windows 32bit using MSVC10.<\/p>\n<p>Say I have following base class:<\/p>\n<pre><code>template\nclass Base\n{\nprotected:\n    T x;\npublic:\n    Base() {}\n    Base(T _x) : x(_x){}\n    virtual bool func() = 0;\n};\n<\/code><\/pre>\n<p>Now, as for the derived class:<\/p>\n<pre><code>template\nclass Derived : public Base\n{\npublic:\n    Derived() {}\n    Derived(T _x) :  Base(_x){}\n    bool func() { return true; };\n};\n<\/code><\/pre>\n<p>Now consider two cases. First, allocate a dynamic array of the Derived class and fill it up with Derived objects, ie:<\/p>\n<pre><code>int main()\n{\n    int size = SOME_LARGE_NUMBER;\n    Derived* d = new Derived[size];\n    for (int i = 0; i &lt; size; i++)\n    {\n        d[i] = Derived(i);\n    }\n\n    \/\/ delete here\n}\n<\/code><\/pre>\n<p>Second, allocate a dynamic array of pointers of the Base class and have them point to actual instances of the Derived class, ie:<\/p>\n<pre><code>int main()\n{\n    int size = SOME_LARGE_NUMBER;\n    Base** d = new Base*[size];\n    for (int i = 0; i &lt; size; i++)\n    {\n        d[i] = new Derived(i);\n    }\n\n    \/\/ delete here\n}\n<\/code><\/pre>\n<p>I set SOME_LARGE_NUMBER in either scenario to 40,000,000. In the first scenario, the program completes fine &#8211; in the second one I get a bad_alloc exception. I am wondering whether this is expected behaviour or whether I am overlooking something here? If so, what&#8217;s a better way of doing this? Note that I get the same problem using <code>vector<\/code> and <code>boost::ptr_vector<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I need some clarification on an issue I don&#8217;t quite understand. Using the two scenarios that follow, I would have thought that the amount of memory allocated would roughly be the same. However, scenario 2 gives me a bad_alloc exception after a while and appears to be chewing up memory like crazy (using Window&#8217;s Task [&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-4499","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4499","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=4499"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4499\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4499"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4499"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4499"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}