{"id":916,"date":"2010-06-01T09:13:45","date_gmt":"2010-06-01T08:13:45","guid":{"rendered":"https:\/\/blogs.mentor.com\/colinwalls\/?p=916"},"modified":"2010-06-01T09:13:45","modified_gmt":"2010-06-01T08:13:45","slug":"static-or-static","status":"publish","type":"post","link":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/2010\/06\/01\/static-or-static\/","title":{"rendered":"Static or static"},"content":{"rendered":"<p>In English, the word &#8220;static&#8221; has a variety of meanings, but they can be summed up by the definition of the adjective: &#8220;pertaining to or characterized by a fixed or stationary condition&#8221;. In the software world, it generally refers to things that do not change over time. In both cases, the opposite may be &#8220;dynamic&#8221;.<\/p>\n<p>In C and C++, the keyword <strong>static<\/strong> is inspired by the broader meaning of the word, but has two, separate uses. In C++, there is even a third use of the keyword. The nuances of these meanings\/uses are not always well understood &#8230;<!--more--><\/p>\n<p>In C\/C++, when you declare a variable [or instantiate an object], the location of the declaration implies the scope of the variable and where in memory it is stored. For example:<\/p>\n<pre>int x;\nvoid fun()\n{\n   int y;\n   ...<\/pre>\n<p>The variable <strong>x<\/strong> has a scope which renders it visible to any function following it in this module and any function in any other module. It is stored in static memory &#8211; i.e. not on the stack or in a register. The variable <strong>y<\/strong> is only available within the function <strong>fun()<\/strong> and is stored on the stack or in a register.<br \/>\nIf we modify the code thus:<\/p>\n<pre>static int x;\nvoid fun()\n{\n   static int y;\n   ...<\/pre>\n<p>The scope of <strong>x<\/strong> is now restricted to functions following it in this module and it is not available to functions outside of this module. Its storage is unchanged. The scope of <strong>y<\/strong> is unchanged, but it is now stored in static memory.<br \/>\nNotice how the same keyword changed the scope of an external variable and the storage of an automatic variable, even though these are quite different attributes of the variables. I feel that an addition keyword <strong>local<\/strong> would be useful to adjust the scope of an external variable.<br \/>\nIn C++, there is a third use for the <strong>static<\/strong> keyword. It can be used to share a member variable among instances of a class. For example:<\/p>\n<pre>class c\n{\n   static int z;\n   ...<\/pre>\n<p>In every instantiation of class <strong>c<\/strong>, the member variable <strong>z<\/strong> is shared; i.e. it refers to the same storage location.<br \/>\nIf you are interested in more information this topic, I authored a <a href=\"http:\/\/www.mentor.com\/products\/embedded_software\/techpubs\/request\/dynamic-memory-allocation-and-fragmentation-in-c-c--57177\" target=\"_blank\" rel=\"noopener noreferrer\">tech paper<\/a> which was published recently and may be useful.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In English, the word &#8220;static&#8221; has a variety of meanings, but they can be summed up by the definition of&#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,376],"industry":[],"product":[],"coauthors":[],"class_list":["post-916","post","type-post","status-publish","format-standard","hentry","category-news","tag-c","tag-dynamic-memory","tag-embedded-software","tag-static"],"_links":{"self":[{"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/916","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/users\/71677"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/comments?post=916"}],"version-history":[{"count":0,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/916\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/media?parent=916"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/categories?post=916"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/tags?post=916"},{"taxonomy":"industry","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/industry?post=916"},{"taxonomy":"product","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/product?post=916"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/coauthors?post=916"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}