source: public/doc/gnu-c/Stack.html@ 02598c2

Last change on this file since 02598c2 was 02598c2, checked in by Mikhail Kirillov <w96k@…>, on Oct 6, 2022 at 12:36:29 PM

Add gnu-c

  • Property mode set to 100644
File size: 5.7 KB
Line 
1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2<html>
3<!-- Copyright (C) 2022 Richard Stallman and Free Software Foundation, Inc.
4
5(The work of Trevis Rothwell and Nelson Beebe has been assigned or
6licensed to the FSF.)
7
8Permission is granted to copy, distribute and/or modify this document
9under the terms of the GNU Free Documentation License, Version 1.3 or
10any later version published by the Free Software Foundation; with the
11Invariant Sections being "GNU General Public License," with the
12Front-Cover Texts being "A GNU Manual," and with the Back-Cover
13Texts as in (a) below. A copy of the license is included in the
14section entitled "GNU Free Documentation License."
15
16(a) The FSF's Back-Cover Text is: "You have the freedom to copy and
17modify this GNU manual. Buying copies from the FSF supports it in
18developing GNU and promoting software freedom." -->
19<!-- Created by GNU Texinfo 6.7, http://www.gnu.org/software/texinfo/ -->
20<head>
21<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
22<title>Stack (GNU C Language Manual)</title>
23
24<meta name="description" content="Stack (GNU C Language Manual)">
25<meta name="keywords" content="Stack (GNU C Language Manual)">
26<meta name="resource-type" content="document">
27<meta name="distribution" content="global">
28<meta name="Generator" content="makeinfo">
29<link href="index.html" rel="start" title="Top">
30<link href="Symbol-Index.html" rel="index" title="Symbol Index">
31<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
32<link href="The-First-Example.html" rel="up" title="The First Example">
33<link href="Iterative-Fibonacci.html" rel="next" title="Iterative Fibonacci">
34<link href="Function-Body.html" rel="prev" title="Function Body">
35<style type="text/css">
36<!--
37a.summary-letter {text-decoration: none}
38blockquote.indentedblock {margin-right: 0em}
39div.display {margin-left: 3.2em}
40div.example {margin-left: 3.2em}
41div.lisp {margin-left: 3.2em}
42kbd {font-style: oblique}
43pre.display {font-family: inherit}
44pre.format {font-family: inherit}
45pre.menu-comment {font-family: serif}
46pre.menu-preformatted {font-family: serif}
47span.nolinebreak {white-space: nowrap}
48span.roman {font-family: initial; font-weight: normal}
49span.sansserif {font-family: sans-serif; font-weight: normal}
50ul.no-bullet {list-style: none}
51-->
52</style>
53
54
55</head>
56
57<body lang="en">
58<span id="Stack"></span><div class="header">
59<p>
60Next: <a href="Iterative-Fibonacci.html" accesskey="n" rel="next">Iterative Fibonacci</a>, Previous: <a href="Recursive-Fibonacci.html" accesskey="p" rel="prev">Recursive Fibonacci</a>, Up: <a href="The-First-Example.html" accesskey="u" rel="up">The First Example</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
61</div>
62<hr>
63<span id="The-Stack_002c-And-Stack-Overflow"></span><h3 class="section">1.2 The Stack, And Stack Overflow</h3>
64<span id="index-stack"></span>
65<span id="index-stack-frame"></span>
66<span id="index-stack-overflow"></span>
67<span id="index-recursion_002c-drawbacks-of"></span>
68
69<span id="index-stack-frame-1"></span>
70<p>Recursion has a drawback: there are limits to how many nested function
71calls a program can make. In C, each function call allocates a block
72of memory which it uses until the call returns. C allocates these
73blocks consecutively within a large area of memory known as the
74<em>stack</em>, so we refer to the blocks as <em>stack frames</em>.
75</p>
76<p>The size of the stack is limited; if the program tries to use too
77much, that causes the program to fail because the stack is full. This
78is called <em>stack overflow</em>.
79</p>
80<span id="index-crash"></span>
81<span id="index-segmentation-fault"></span>
82<p>Stack overflow on GNU/Linux typically manifests itself as the
83<em>signal</em> named <code>SIGSEGV</code>, also known as a &ldquo;segmentation
84fault.&rdquo; By default, this signal terminates the program immediately,
85rather than letting the program try to recover, or reach an expected
86ending point. (We commonly say in this case that the program
87&ldquo;crashes&rdquo;). See <a href="Signals.html">Signals</a>.
88</p>
89<p>It is inconvenient to observe a crash by passing too large
90an argument to recursive Fibonacci, because the program would run a
91long time before it crashes. This algorithm is simple but
92ridiculously slow: in calculating <code>fib (<var>n</var>)</code>, the number of
93(recursive) calls <code>fib (1)</code> or <code>fib (2)</code> that it makes equals
94the final result.
95</p>
96<p>However, you can observe stack overflow very quickly if you use
97this function instead:
98</p>
99<div class="example">
100<pre class="example">int
101fill_stack (int n)
102{
103 if (n &lt;= 1) /* <span class="roman">This limits the depth of recursion.</span> */
104 return 1;
105 else
106 return fill_stack (n - 1);
107}
108</pre></div>
109
110<p>Under gNewSense GNU/Linux on the Lemote Yeeloong, without optimization
111and using the default configuration, an experiment showed there is
112enough stack space to do 261906 nested calls to that function. One
113more, and the stack overflows and the program crashes. On another
114platform, with a different configuration, or with a different
115function, the limit might be bigger or smaller.
116</p>
117<hr>
118<div class="header">
119<p>
120Next: <a href="Iterative-Fibonacci.html" accesskey="n" rel="next">Iterative Fibonacci</a>, Previous: <a href="Recursive-Fibonacci.html" accesskey="p" rel="prev">Recursive Fibonacci</a>, Up: <a href="The-First-Example.html" accesskey="u" rel="up">The First Example</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
121</div>
122
123
124
125</body>
126</html>
Note: See TracBrowser for help on using the repository browser.