{"id":2243,"date":"2016-12-10T00:01:31","date_gmt":"2016-12-10T08:01:31","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2243"},"modified":"2016-12-17T08:05:43","modified_gmt":"2016-12-17T16:05:43","slug":"behold-the-stack-part-iv","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2243","title":{"rendered":"Behold the Stack, Part IV"},"content":{"rendered":"<p>To simulate stack storage in a C program, you need to emulate the four basic parts of a stack:<\/p>\n<ol>\n<li>The stack storage<\/li>\n<li>A stack pointer<\/li>\n<li>A function to push a value onto the stack<\/li>\n<li>A function to pop a value from the stack<\/li>\n<\/ol>\n<p>Further, you need to implement controls that prevent a stack overflow or underflow. And you can get real fancy, but those four items are the basics.<br \/>\n<!--more--><br \/>\nIn my code, I assign an array as stack storage. Unlike a processor stack, which is just a chunk of memory, in C an array must be typed, such as an integer array. The stack pointer variable must match the array type. And you don&#8217;t need to use a pointer; you could use an index into the array, but I&#8217;m using a pointer because, well, it&#8217;s called a stack &#8220;pointer,&#8221; isn&#8217;t it?<\/p>\n<p>The two functions are traditionally named <em>push()<\/em> and <em>pop()<\/em>. The <em>push()<\/em> function requires an argument, the value to push onto the stack. The <em>pop()<\/em> function returns a value.<\/p>\n<p>It&#8217;s easiest to code the stack emulation with the stack array and pointer as global variables. Otherwise, the stack array and pointer are required as arguments for both <em>push()<\/em> and <em>pop()<\/em> functions. For simplicity&#8217;s sake, I use global variables in my examples.<\/p>\n<p>Here is how I&#8217;d declare the stack array and stack pointer as global variables:<\/p>\n<pre class=\"screen\">\r\n#define STACK_SIZE 16\r\n\r\nint stack[STACK_SIZE];\r\nint *sp;<\/pre>\n<p>Array <code>stack[]<\/code> represents 16 <em>int<\/em> values. Integer pointer variable <code>*sp<\/code> references the &#8220;top&#8221; of the stack.<\/p>\n<p>The <em>push()<\/em> function sends a value to the top of the stack. Further, it tests to ensure that a stack overflow doesn&#8217;t occur:<\/p>\n<pre class=\"screen\">\r\nvoid push(int v)\r\n{\r\n    if(sp == stack+STACK_SIZE)\r\n    {\r\n        puts(\"Stack Overflow\");\r\n    }\r\n    else\r\n    {\r\n        sp++;\r\n        *sp = v;\r\n    }\r\n}<\/pre>\n<p>In this function, value <code>v<\/code> is pushed to the top of the stack. Before that, however, an <em>if<\/em> statement tests to ensure that the stack isn&#8217;t full. If so, the Stack Overflow message is displayed and the function returns. In the real world, the program would terminate at this point; memory overflows are serious business. In fact, you may have already coded programs that crash with a &#8220;Stack Overflow&#8221; message (especially when you do recursion).<\/p>\n<p>The <em>else<\/em> statements in the <em>push()<\/em> function increment the <code>sp<\/code> (stack pointer) variable and set value <code>v<\/code> to that memory location.<\/p>\n<p>In the <em>push()<\/em> function, variable <code>sp<\/code> always points at the top the stack. It does not point to the location of the next value. This is exactly how the <code>esp<\/code> register works in the processor.<\/p>\n<p>To fetch a value from the stack, the <em>pop()<\/em> function is used. It requires no arguments in my example, because the <code>stack[]<\/code> array and <code>sp<\/code> pointer are global:<\/p>\n<pre class=\"screen\">\r\nint pop(void)\r\n{\r\n    int s;\r\n\r\n    s = *sp;\r\n    if( sp == stack)\r\n    {\r\n        puts(\"Stack Empty\");\r\n    }\r\n    else\r\n    {\r\n        sp--;\r\n    }\r\n    return(s);\r\n}<\/pre>\n<p>The value at the top of the stack is saved in variable <code>s<\/code>. (It doesn&#8217;t have to be, but I wrote the function to be more readable.) The <em>if<\/em> comparison confirms whether the stack isn&#8217;t empty. If it is, an underflow message is displayed and the function returns. Otherwise, the <code>sp<\/code> variable is decremented and the value saved in variable <code>s<\/code> is returned.<\/p>\n<p>Without variable <code>s<\/code>, the <em>pop()<\/em> function looks like this:<\/p>\n<pre class=\"screen\">\r\nint pop(void)\r\n{\r\n    if( sp == stack)\r\n    {\r\n        puts(\"Stack Empty\");\r\n        return(0);\r\n    }\r\n    else\r\n    {\r\n        return(*sp--);\r\n    }\r\n}<\/pre>\n<p>Both <em>if<\/em> and <em>else<\/em> need <em>return<\/em> statements because the <em>pop()<\/em> is an <em>int<\/em> function. I return 0 on the stack empty condition, though in a real program you might just use <em>exit()<\/em>; it&#8217;s dangerous to assume that 0 would be on the stack when the stack is empty.<\/p>\n<p>The <code>*sp--<\/code> thing in the second <em>return<\/em> statement first fetches the value from the stack, then it decrements the <code>sp<\/code> pointer.<\/p>\n<p>In <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2249\">next week&#8217;s Lesson<\/a>, I demonstrate how to use these four elements in a program to simulate stack storage.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To simulate stack storage, your code needs four items. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2243\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-2243","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2243","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2243"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2243\/revisions"}],"predecessor-version":[{"id":2268,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2243\/revisions\/2268"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2243"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2243"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2243"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}