{"id":5063,"date":"2014-03-30T18:31:16","date_gmt":"2014-03-30T18:31:16","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/memory-questions-and-functions-collection-of-common-programming-errors\/"},"modified":"2014-03-30T18:31:16","modified_gmt":"2014-03-30T18:31:16","slug":"memory-questions-and-functions-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/memory-questions-and-functions-collection-of-common-programming-errors\/","title":{"rendered":"Memory questions and functions-Collection of common programming errors"},"content":{"rendered":"<p>First, I will let you write your own code that is the best way to learn. But, I will answer the theory behind these questions&#8230;<\/p>\n<p>1) when raising the number 2 to the nth power, is the same as multiplying 2 by itself n times. But multiplying by 2 is same as doubling a number, and computers store their numbers in base 2. For example, the binary value for 6 == 0b00110 but when all of the bits are shifted to the left by 1, then 12 == 0b01100, which is same as 6*2. So, for example, 16 == 4^2 = 4*4 = 4*(2*2)or in binary 16 == 0b010000 == 0b001000*2 = (0b00100)*2*2.<\/p>\n<p>2) when a variable is declared outside of all routines it is in &#8220;global&#8221; storage, also probably called &#8220;Data&#8221;. When a variable is declared within a routine it is an &#8220;automatic&#8221; variable (meaning that it is automatically) and is allocated on the stack. When a variable is explicitly allocated, using <code>malloc<\/code> as one example, then it is allocated on the heap.<\/p>\n<p>In C, pointers have two data components or storage elements. First, is the pointer itself which typically uses only 4 bytes. Then the data which is whatever length was allocated for the data. To say anything more will repeat information provided by other answers!<\/p>\n<p>It is a good programming practice to <code>free<\/code> all storage that has been allocated on the heap. Although, the system should deallocate that storage when <code>main<\/code> exits, this is NOT always guaranteed especially on small or embedded systems. Thus, the code example should end as:<\/p>\n<pre><code>    free(c);\n    return 0;\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>First, I will let you write your own code that is the best way to learn. But, I will answer the theory behind these questions&#8230; 1) when raising the number 2 to the nth power, is the same as multiplying 2 by itself n times. But multiplying by 2 is same as doubling a number, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5063","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5063","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=5063"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5063\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5063"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5063"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5063"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}