{"id":6910,"date":"2014-05-27T10:49:33","date_gmt":"2014-05-27T09:49:33","guid":{"rendered":"https:\/\/blogs.mentor.com\/colinwalls\/?p=6910"},"modified":"2026-03-26T16:43:27","modified_gmt":"2026-03-26T20:43:27","slug":"problems-with-pointers-out-of-scope-out-of-mind","status":"publish","type":"post","link":"https:\/\/blogs.sw.siemens.com\/embedded-software\/2014\/05\/27\/problems-with-pointers-out-of-scope-out-of-mind\/","title":{"rendered":"Problems with pointers: out of scope, out of mind"},"content":{"rendered":"<p>Last week I conducted a series of 5 <a href=\"http:\/\/www.designnews.com\/lecture-calendar.asp\" target=\"_blank\" rel=\"noopener noreferrer\">online lectures about C++ for embedded applications<\/a>. [You can still access the materials and recordings from this site.] As with any such activity, although I hope that I am imparting information and advice, I also expect to get questions and ideas back. I was not disappointed. There were many smart questions that made me think [= good!]. There were no dumb questions that I know of, as the only dumb question is the one that you fail to ask.<\/p>\n<p>It got particularly interesting when C++ constructors and destructors get mixed up with pointers &#8230;<!--more--><\/p>\n<p>First off, there is a little confusion about the difference between the scope of a variable\/object and its actual existence. For &#8220;existence&#8221;, read &#8220;validity&#8221; because the memory occupied by a variable never goes away; the time at which the memory is actually allocated to the variable varies. I will illustrate this point with some C code.<\/p>\n<p><a href=\"https:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/51\/2014\/05\/s1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignright wp-image-6911\" src=\"https:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/51\/2014\/05\/s1-476x800.png\" alt=\"s1\" width=\"200\" height=\"335\" \/><\/a>In this code, the array <strong>arr<\/strong> has a scope which is limited to inside the function <strong>fun()<\/strong>. The memory it uses is also allocated at the start of this function and deallocated when the code returns. However, this code, though syntactically correct, has a glaring error: it returns a pointer to <strong>arr<\/strong> so that the code in function <strong>f2()<\/strong> can access the array after the return from <strong>fun()<\/strong>. But, since this memory has now been deallocated, the assignment to <strong>p[3]<\/strong> is not valid and could produce an &#8220;interesting&#8221; error. Remember that pointers, though a powerful language feature in C\/C++, are the most common source of errors.<\/p>\n<p>A small change to the code rectifies this fault.<\/p>\n<p><a href=\"https:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/51\/2014\/05\/s2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft wp-image-6913\" src=\"https:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/51\/2014\/05\/s2-520x604.png\" alt=\"s2\" width=\"200\" height=\"232\" \/><\/a>By declaring <strong>arr<\/strong> to be <strong>static<\/strong> the time frame of its existence is changed. The memory is now allocated at build time so it is in existence before, during and, most importantly, after the execution of <strong>fun()<\/strong> so the assignment to <strong>p[3]<\/strong> is now valid. The <em>scope<\/em> of <strong>arr<\/strong> is unchanged: it is only accessible inside <strong>fun()<\/strong>.<\/p>\n<p>All of this would be equally true in C++, but we have the additional issue of constructor and destructor functions, which are automatically executed when an object is created and destroyed. Here is some similar code in C++.<\/p>\n<p><a href=\"https:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/51\/2014\/05\/s3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignright wp-image-6915\" src=\"https:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/51\/2014\/05\/s3.png\" alt=\"s3\" width=\"200\" height=\"327\" \/><\/a>This is much the same as the first example, except that we have an object of\u00a0type <strong>ccc<\/strong> instead of an <strong>int<\/strong>. Some key things do not change: the object <strong>z<\/strong> still has a scope and existence that is confined to the function <strong>fun()<\/strong>. However, there is now the question of the constructor and destructor functions for <strong>z<\/strong>. These are run when the object is created and when it is destroyed, which occurs on return from <strong>fun()<\/strong>. So, any subsequence access to the object, via the returned pointer, is a problem because the destructor will have deallocated all the object&#8217;s resources.<\/p>\n<p>In the same way as with the C example, the object could be made <strong>static<\/strong> and all would be well.<\/p>\n<p>There is another option: use dynamic memory allocation to create the variable\/object; the deallocation of resources is then under the programmer&#8217;s control. In C, this would require the use of <strong>malloc()<\/strong>, but in C++ it may be done more neatly using <strong>new<\/strong>.<\/p>\n<p><a href=\"https:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/51\/2014\/05\/s4.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft wp-image-6916\" src=\"https:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/51\/2014\/05\/s4-520x537.png\" alt=\"s4\" width=\"200\" height=\"207\" \/><\/a>The constructor for the created <strong>ccc<\/strong> object is executed when the <strong>new<\/strong> is invoked. The destructor is executed at some later time when the corresponding <strong>delete<\/strong> is applied. This approach has the advantage of giving control over the running of constructor\/destructor, but their automatic execution on scope helps to avoid memory leak style errors.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Last week I conducted a series of 5 online lectures about C++ for embedded applications. [You can still access the&#8230;<\/p>\n","protected":false},"author":71677,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spanish_translation":"","french_translation":"","german_translation":"","italian_translation":"","polish_translation":"","japanese_translation":"","chinese_translation":"","footnotes":""},"categories":[1],"tags":[313,334,300],"industry":[],"product":[],"coauthors":[],"class_list":["post-6910","post","type-post","status-publish","format-standard","hentry","category-news","tag-c","tag-dynamic-memory","tag-embedded-software"],"_links":{"self":[{"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/6910","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/users\/71677"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/comments?post=6910"}],"version-history":[{"count":1,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/6910\/revisions"}],"predecessor-version":[{"id":10274,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/6910\/revisions\/10274"}],"wp:attachment":[{"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/media?parent=6910"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/categories?post=6910"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/tags?post=6910"},{"taxonomy":"industry","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/industry?post=6910"},{"taxonomy":"product","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/product?post=6910"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/coauthors?post=6910"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}