{"id":305,"date":"2006-08-31T20:55:12","date_gmt":"2006-09-01T03:55:12","guid":{"rendered":"http:\/\/multimedia.cx\/eggs\/variable-declaration-guidelines\/"},"modified":"2020-07-25T23:28:48","modified_gmt":"2020-07-26T06:28:48","slug":"variable-declaration-guidelines","status":"publish","type":"post","link":"https:\/\/multimedia.cx\/eggs\/variable-declaration-guidelines\/","title":{"rendered":"Variable Declaration Guidelines"},"content":{"rendered":"<p>Back in 2000, I came across this Advogato article about <a href=\"http:\/\/advogato.org\/article\/97.html\">proper coding guidelines<\/a> for the coming wave of 64-bit machines. The most interesting part, I thought, was <a href=\"http:\/\/advogato.org\/article\/97.html#2\">comment #2 (&#8220;C is portable, if you let it be&#8221;)<\/a> which offers some very sane guidelines for declaring variable types to just allow the compiler to do its job effectively. This is why I usually just declare int&#8217;s for numbers rather than uint32_t&#8217;s everywhere. There is often no reason to try to force particular types.<\/p>\n<p>Don&#8217;t think that you&#8217;re saving space by declaring a uint8_t instead of an int&#8211; chances are that you aren&#8217;t. I&#8217;ve disassembled enough C code compiled into 32-bit x86 machine code to know that a compiler will usually allocate 32 bits for that 8-bit variable. In fact, here is a small piece of code to drive the point home:<\/p>\n<p>stack.c:<br \/>\n<script src=\"https:\/\/gist.github.com\/multimediamike\/3b734488442b473835f8d9492793a6c5.js\"><\/script><\/p>\n<p>Compile with: gcc -Wall stack.c -o stack<br \/>\nDisassemble with: objdump -d -Mintel stack<br \/>\nKey parts:<\/p>\n<pre>\r\n080483a0 < main >:\r\n 80483a0:   55                  push   ebp\r\n 80483a1:   89 e5               mov    ebp,esp\r\n 80483a3:   83 ec 08            sub    esp,0x8\r\n 80483a6:   83 e4 f0            and    esp,0xfffffff0\r\n 80483a9:   b8 00 00 00 00      mov    eax,0x0\r\n 80483ae:   29 c4               sub    esp,eax\r\n 80483b0:   e8 07 ff ff ff      call   80482bc < random @plt >\r\n 80483b5:   88 45 ff            mov    BYTE PTR [ebp-1],al\r\n 80483b8:   66 0f be 45 ff      movsx  ax,BYTE PTR [ebp-1]\r\n 80483bd:   40                  inc    eax\r\n 80483be:   66 89 45 fc         mov    WORD PTR [ebp-4],ax\r\n<\/pre>\n<p>Notice that, despite strictly needing only 3 bytes of local variable storage, 8 bytes were allocated from the stack. 32-bit machines like the i386 really, really like dealing with 32-bit quantities.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Back in 2000, I came across this Advogato article about proper coding guidelines for the coming wave of 64-bit machines. The most interesting part, I thought, was comment #2 (&#8220;C is portable, if you let it be&#8221;) which offers some very sane guidelines for declaring variable types to just allow the compiler to do its [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[28],"tags":[],"class_list":["post-305","post","type-post","status-publish","format-standard","hentry","category-programming"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/posts\/305","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/comments?post=305"}],"version-history":[{"count":2,"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/posts\/305\/revisions"}],"predecessor-version":[{"id":4627,"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/posts\/305\/revisions\/4627"}],"wp:attachment":[{"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/media?parent=305"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/categories?post=305"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/tags?post=305"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}