{"id":54,"date":"2012-12-12T15:55:31","date_gmt":"2012-12-12T15:55:31","guid":{"rendered":"http:\/\/news.innerfire.net\/?p=54"},"modified":"2018-07-25T15:31:36","modified_gmt":"2018-07-25T15:31:36","slug":"const-vs-static-const-in-c-with-gcc","status":"publish","type":"post","link":"https:\/\/news.innerfire.net\/?p=54","title":{"rendered":"const vs static const in C with GCC"},"content":{"rendered":"<p>Something I&#8217;ve been wondering but have yet to see a good explanation for is if there is a difference between the way GCC handles const and static const.\u00a0 Now in theory the compiler should handle them the same way but does it?\u00a0 My web search didn&#8217;t really come up with much so I decided to test for myself.<\/p>\n<p>A quick test using GCC 4.7.2:<\/p>\n<p>const.c<\/p>\n<pre class=\"brush: c; gutter: true\">#include &lt;string.h&gt;\r\n#include &lt;stdio.h&gt;\r\n\r\nvoid test()\r\n{\r\n        const char i[] = \"not changed\";\r\n        char *j;\r\n        printf(\"before %s\\n\", i);\r\n\r\n        j = i;\r\n\r\n        strcpy(j, \"changed\");\r\n        printf(\"after i=%s\\n\", i);\r\n}\r\n\r\nint main()\r\n{\r\n        test();\r\n        test();\r\n}<\/pre>\n<p>staticconst.c<\/p>\n<pre class=\"brush: c; gutter: true\">#include &lt;string.h&gt;\r\n#include &lt;stdio.h&gt;\r\n\r\nvoid test()\r\n{\r\n        static const char i[] = \"not changed\";\r\n        char *j;\r\n        printf(\"before %s\\n\", i);\r\n\r\n        j = i;\r\n\r\n        strcpy(j, \"changed\");\r\n        printf(\"after i=%s\\n\", i);\r\n}\r\n\r\nint main()\r\n{\r\n        test();\r\n        test();\r\n}<\/pre>\n<p>How each turns out:<\/p>\n<p>const.c<\/p>\n<p>~$ gcc const.c -o const -g<br \/>\nconst.c: In function \u2018test\u2019:<br \/>\nconst.c:10:4: warning: assignment discards \u2018const\u2019 qualifier from pointer target type [enabled by default]<br \/>\n~$ .\/const<br \/>\nbefore not changed<br \/>\nafter i=changed<br \/>\nbefore not changed<br \/>\nafter i=changed<\/p>\n<p>Not only can I change the variable, but it resets it at each function call so this means it is reallocating the variable each time.<\/p>\n<p>staticconst.c<\/p>\n<p>~$ gcc staticconst.c -o staticconst -g<br \/>\nstaticconst.c: In function \u2018test\u2019:<br \/>\nstaticconst.c:10:4: warning: assignment discards \u2018const\u2019 qualifier from pointer target type [enabled by default]<br \/>\n.\/staticconst<br \/>\nbefore not changed<br \/>\nSegmentation fault (core dumped)<\/p>\n<p>gdb shows the following:<\/p>\n<p>Core was generated by `.\/staticconst&#8217;.<br \/>\nProgram terminated with signal 11, Segmentation fault.<br \/>\n#0\u00a0 0x0000000000400534 in test () at staticconst.c:12<br \/>\n12\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0strcpy(j, &#8220;changed&#8221;);<\/p>\n<p>As you can see static const is stored somewhere actually read only and being static it is not being reallocated each time the function is called making this version more efficient.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Something I&#8217;ve been wondering but have yet to see a good explanation for is if there is a difference between the way GCC handles const and static const.\u00a0 Now in theory the compiler should handle them the same way but does it?\u00a0 My web search didn&#8217;t really come up with much so I decided to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-54","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/news.innerfire.net\/index.php?rest_route=\/wp\/v2\/posts\/54","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/news.innerfire.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/news.innerfire.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/news.innerfire.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/news.innerfire.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=54"}],"version-history":[{"count":7,"href":"https:\/\/news.innerfire.net\/index.php?rest_route=\/wp\/v2\/posts\/54\/revisions"}],"predecessor-version":[{"id":105,"href":"https:\/\/news.innerfire.net\/index.php?rest_route=\/wp\/v2\/posts\/54\/revisions\/105"}],"wp:attachment":[{"href":"https:\/\/news.innerfire.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=54"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/news.innerfire.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=54"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/news.innerfire.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=54"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}