{"id":7652,"date":"2026-07-11T00:01:39","date_gmt":"2026-07-11T07:01:39","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=7652"},"modified":"2026-07-04T13:30:08","modified_gmt":"2026-07-04T20:30:08","slug":"recursion-until-death","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=7652","title":{"rendered":"Recursion Until Death!"},"content":{"rendered":"<p>When I first learned Assembly Language, I had a fear of blowing up the stack. Remember, back in the 8-bit microcomputer era that memory space was tight, maybe only part of 64K. A stack would have 1K of storage, if that. I think the stack size on my TRS-80 was 256 bytes. (An address was only two bytes wide.) This limitation meant that pushing too much data on the stack would blow up the computer.<br \/>\n<!--more--><br \/>\nNothing would explode, of course; I love hyperbole. Even so, I was leery of pushing too much data on the stack. So, while I understood the concept of recursion, I remained fearful that if a function called itself too often the stack would explode and I&#8217;d be sweeping up bits from the floor behind the computer.<\/p>\n<p>That never happened.<\/p>\n<p>It never happened because I was careful. But when I obtained my first 16-bit computer, I harbored no fear of blowing up the stack. In fact, I must have at some point coded a program like the following just to test how robust the stack can be:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2026_07_11-Lesson.c\" rel=\"noopener\" target=\"_blank\">2026_07_11-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nvoid recurse(int i)\r\n{\r\n    printf(\"%d\\n\",i);\r\n    recurse(i+1);\r\n}\r\n\r\nint main()\r\n{\r\n    recurse(0);\r\n\r\n    return 0;\r\n}<\/pre>\n<p>This code provides an example of recursion. A poor example. The problem is that recursion doesn&#8217;t ever unwind: The function keeps calling itself until no more return addresses can be pushed onto the stack. Boom.<\/p>\n<p>The code compiles cleanly under <em>gcc<\/em>, but <em>clang<\/em> issues an &#8220;infinite recursion&#8221; warning. Regardless, a program is built.<\/p>\n<p>Here is the tail end of output from a sample run:<\/p>\n<pre>261834\r\n261835\r\n261836\r\n261837\r\n261838\r\nSegmentation fault (core dumped)<\/pre>\n<p>On my test computer, the function called itself 261,838 times before the stack could hold no more values. The &#8220;Segmentation fault&#8221; means that an attempt was made to access memory outside of the program&#8217;s allocated space (the &#8220;segment&#8221;). The core is dumped. The program stops.<\/p>\n<p>Other computers show different final values in the output, based on allocated stack space.<\/p>\n<p>So how much stack space is there? To find out in Linux, use the <em>ulimit -s<\/em> command at the terminal:<\/p>\n<pre>$ ulimit -s\r\n8192<\/pre>\n<p>The <code>-s<\/code> option outputs the stack size in kilobytes. (Use <code>ulimit -a<\/code> to see all limits set for the current terminal configuration.)<\/p>\n<p>According to the output, the current terminal has a stack size of 8192K, or 8,388,608 bytes, which is a lot of storage.<\/p>\n<p>It&#8217;s possible to use <em>ulimit<\/em> to change the stack size. For example:<\/p>\n<pre>ulimit -s 16384<\/pre>\n<p>When I made this change and ran the program again, here are the last few lines of output:<\/p>\n<pre>524090\r\n524091\r\n524092\r\n524093\r\nSegmentation fault (core dumped)<\/pre>\n<p>Obviously, the result is larger because the stack is larger and it took longer for recursion to explode.<\/p>\n<p>By the way, changing the stack size affects only the current terminal configuration and its programs or &#8220;children.&#8221; Permanent changes to the stack size are made in the <code>\/etc\/security\/limits.conf<\/code> file.<\/p>\n<p>Recursion is something you can safely employ in your code &mdash; as long as it successfully unwinds itself. If necessary, you can allocate more stack storage, but I would be interested in see what type of program would merit such an increase.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How many times can a function call itself before implosion? <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7652\">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-7652","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\/7652","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=7652"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7652\/revisions"}],"predecessor-version":[{"id":7681,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7652\/revisions\/7681"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7652"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7652"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7652"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}