{"id":323,"date":"2019-02-19T21:52:38","date_gmt":"2019-02-20T05:52:38","guid":{"rendered":"http:\/\/35.243.195.209\/?p=323"},"modified":"2019-03-02T11:33:00","modified_gmt":"2019-03-02T19:33:00","slug":"allocate-memory-in-constructors-and-destructors","status":"publish","type":"post","link":"https:\/\/nanzhou.cc\/index.php\/2019\/02\/19\/allocate-memory-in-constructors-and-destructors\/","title":{"rendered":"Allocate Memory in Constructors and Destructors"},"content":{"rendered":"<h2>Summary<\/h2>\n<p>In this post, I will introduce the way to define a class which contains memory allocation. I will focus on constructors and destructors in this post.<\/p>\n<h2>Conclusion<\/h2>\n<p>I present the conclusion first as usual: whenever the implementation of a class involves dynamically allocated memory, the class should explicitly define:<\/p>\n<ol>\n<li>a destructor which frees the allocated memory;<\/li>\n<li>a copy constructor;<\/li>\n<li>an assignment operator;<\/li>\n<\/ol>\n<h3>Constructors and Destructors<\/h3>\n<ol>\n<li>A class has at least one constructor, and has exactly one destructor;<\/li>\n<li>Constructors and destructors are not inherited;<\/li>\n<li>Unless Derived&#8217;s constructor explicitly calls one of Base&#8217;s constructor, the default constructor from Base will be called automatically before Derived&#8217;s constructor body (the idea being that Base needs to be initialized before Derived gets created);<\/li>\n<li>After Derived&#8217;s destructors exits (or the default destructor exits), B&#8217;s destructor will automatically call A&#8217;s destructor.<\/li>\n<\/ol>\n<h3>Assignment Operator and Copy Constructor<\/h3>\n<p>It relates to shallow copy and deep copy and the default copy constructor performs shallow copy which may cause problems. I will discuss it in another post.<\/p>\n<h2>Details<\/h2>\n<p>I come across this problem when designing the data structures in Project <a href=\"http:\/\/35.243.195.209\/index.php\/projects\/platforms-for-deep-learning\/\">Knerex<\/a>. After some research, I figure out the solution. For better understanding, I provide the following snippets for your reference.<\/p>\n<h3>Construtors and Destructors<\/h3>\n<p>In the destructors, we should free all the allocated memory.<\/p>\n<pre><code class=\"c\">class Base {\nprivate:\n    int *base_array;\npublic:\n    Base() {\n        cout &lt;&lt; \"base_array has been allocated!\" &lt;&lt; endl;\n        base_array = new int[10];\n    }\n    ~Base() {\n        delete base_array;\n        cout &lt;&lt; \"base_array has been deleted!\" &lt;&lt; endl;\n    }\n};\n\nclass Derived : public Base {\nprivate:\n    int *derived_array;\npublic:\n    Derived() {\n        cout &lt;&lt; \"derived_array has been allocated!\" &lt;&lt; endl;\n        derived_array = new int[10];\n    }\n    ~Derived() {\n        delete derived_array;\n        cout &lt;&lt; \"derived_array has been deleted!\" &lt;&lt; endl;\n    }\n};\n\nint main() {\n    {\n        Base base;\n    }\n        \/*\n        base_array has been allocated!\n        base_array has been deleted!\n        *\/\n    {\n        Derived derived;\n    }\n        \/*\n        base_array has been allocated!\n        derived_array has been allocated!\n        derived_array has been deleted!\n        base_array has been deleted!\n        *\/\n}\n<\/code><\/pre>\n<h2>Reference<\/h2>\n<ol>\n<li><a href=\"http:\/\/pages.cs.wisc.edu\/~siff\/CS367\/Notes\/dynamic-memory.html\">wisc.edu<\/a><\/li>\n<li><a href=\"https:\/\/stackoverflow.com\/questions\/14184341\/c-constructor-destructor-inheritance\">stackoverflow<\/a><\/li>\n<li><a href=\"https:\/\/stackoverflow.com\/questions\/1734628\/copy-constructor-and-operator-overload-in-c-is-a-common-function-possible\">stackoverflow<\/a><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Summary In this post, I will introduce the way to define a class which contains memory allocation. I will focus on constructors and destructors in this post. Conclusion I present the conclusion first as usual: whenever the implementation of a class involves dynamically allocated memory, the class should explicitly define: a destructor which frees the&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[],"class_list":["post-323","post","type-post","status-publish","format-standard","hentry","category-software-engineering"],"_links":{"self":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/323","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/comments?post=323"}],"version-history":[{"count":6,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/323\/revisions"}],"predecessor-version":[{"id":340,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/323\/revisions\/340"}],"wp:attachment":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/media?parent=323"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/categories?post=323"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/tags?post=323"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}