{"id":330,"date":"2017-05-27T18:59:56","date_gmt":"2017-05-27T18:59:56","guid":{"rendered":"http:\/\/www.max-sperling.bplaced.net\/?p=330"},"modified":"2024-02-16T11:12:09","modified_gmt":"2024-02-16T11:12:09","slug":"memory-management-c","status":"publish","type":"post","link":"http:\/\/www.max-sperling.bplaced.net\/?p=330","title":{"rendered":"Memory Management (C++)"},"content":{"rendered":"<p><strong>1. Automatic allocation &#8211; Stack<\/strong><br \/>\nObjects created on the stack, will be cleaned up automatically after they have run out of scope. This happens in excact reverse order than they have been created. It has a higher performance then the dynamic allocation, but normally just 1MB of memory.<\/p>\n<pre class=\"brush: cpp; gutter: false; title: ; notranslate\" title=\"\">\r\nint answer = 42;\r\n<\/pre>\n<hr>\n<p><strong>2. Dynamic allocation &#8211; Heap<\/strong><br \/>\nObjects created by keywords like &#8216;malloc&#8217; or &#8216;new&#8217; will get allocated on the heap. It doesn&#8217;t get cleaned up automatically and needs to be deallocated by the keywords &#8216;free&#8217; or &#8216;delete&#8217;. That kind of allocation possibly leads to memory leaks, but provides way more memory.<\/p>\n<pre class=\"brush: cpp; gutter: false; title: C-Style; notranslate\" title=\"C-Style\">\r\nint *answer = calloc(42, sizeof(int));\r\nfree(answer);\r\n<\/pre>\n<pre class=\"brush: cpp; gutter: false; title: C++-Style; notranslate\" title=\"C++-Style\">\r\nint *answer = new int(42);\r\ndelete answer;\r\n<\/pre>\n<hr>\n<p><strong>3. Static allocation &#8211; Data<\/strong><br \/>\nStatic objects are stored in the data section at statup and they have a lifetime of the application.<\/p>\n<pre class=\"brush: cpp; gutter: false; title: ; notranslate\" title=\"\">\r\nstatic int answer = 42;\r\n<\/pre>\n<hr>\n<p><strong>4. Code instructions &#8211; Text<\/strong><br \/>\nThe machine code instructions are stored in the text segment. Normally they will be loaded on demand.<\/p>\n<hr>\n<p><u>Further informations:<\/u><br \/>\n&#8211; <a href=\"http:\/\/www.max-sperling.bplaced.net\/?p=2019\">More details (+ Asm instructions)<\/a><br \/>\n&#8211; <a href=\"http:\/\/www.max-sperling.bplaced.net\/?p=1658\">Exmple for con-\/destruction order<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. Automatic allocation &#8211; Stack Objects created on the stack, will be cleaned up automatically after they have run out<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false},"categories":[28],"tags":[],"_links":{"self":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/330"}],"collection":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=330"}],"version-history":[{"count":1,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/330\/revisions"}],"predecessor-version":[{"id":16782,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/330\/revisions\/16782"}],"wp:attachment":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=330"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=330"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=330"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}