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
|
---|
6 | licensed to the FSF.)
|
---|
7 |
|
---|
8 | Permission is granted to copy, distribute and/or modify this document
|
---|
9 | under the terms of the GNU Free Documentation License, Version 1.3 or
|
---|
10 | any later version published by the Free Software Foundation; with the
|
---|
11 | Invariant Sections being "GNU General Public License," with the
|
---|
12 | Front-Cover Texts being "A GNU Manual," and with the Back-Cover
|
---|
13 | Texts as in (a) below. A copy of the license is included in the
|
---|
14 | section entitled "GNU Free Documentation License."
|
---|
15 |
|
---|
16 | (a) The FSF's Back-Cover Text is: "You have the freedom to copy and
|
---|
17 | modify this GNU manual. Buying copies from the FSF supports it in
|
---|
18 | developing 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 | <!--
|
---|
37 | a.summary-letter {text-decoration: none}
|
---|
38 | blockquote.indentedblock {margin-right: 0em}
|
---|
39 | div.display {margin-left: 3.2em}
|
---|
40 | div.example {margin-left: 3.2em}
|
---|
41 | div.lisp {margin-left: 3.2em}
|
---|
42 | kbd {font-style: oblique}
|
---|
43 | pre.display {font-family: inherit}
|
---|
44 | pre.format {font-family: inherit}
|
---|
45 | pre.menu-comment {font-family: serif}
|
---|
46 | pre.menu-preformatted {font-family: serif}
|
---|
47 | span.nolinebreak {white-space: nowrap}
|
---|
48 | span.roman {font-family: initial; font-weight: normal}
|
---|
49 | span.sansserif {font-family: sans-serif; font-weight: normal}
|
---|
50 | ul.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>
|
---|
60 | Next: <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> [<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
|
---|
71 | calls a program can make. In C, each function call allocates a block
|
---|
72 | of memory which it uses until the call returns. C allocates these
|
---|
73 | blocks 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
|
---|
77 | much, that causes the program to fail because the stack is full. This
|
---|
78 | is 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 “segmentation
|
---|
84 | fault.” By default, this signal terminates the program immediately,
|
---|
85 | rather than letting the program try to recover, or reach an expected
|
---|
86 | ending point. (We commonly say in this case that the program
|
---|
87 | “crashes”). See <a href="Signals.html">Signals</a>.
|
---|
88 | </p>
|
---|
89 | <p>It is inconvenient to observe a crash by passing too large
|
---|
90 | an argument to recursive Fibonacci, because the program would run a
|
---|
91 | long time before it crashes. This algorithm is simple but
|
---|
92 | ridiculously 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
|
---|
94 | the final result.
|
---|
95 | </p>
|
---|
96 | <p>However, you can observe stack overflow very quickly if you use
|
---|
97 | this function instead:
|
---|
98 | </p>
|
---|
99 | <div class="example">
|
---|
100 | <pre class="example">int
|
---|
101 | fill_stack (int n)
|
---|
102 | {
|
---|
103 | if (n <= 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
|
---|
111 | and using the default configuration, an experiment showed there is
|
---|
112 | enough stack space to do 261906 nested calls to that function. One
|
---|
113 | more, and the stack overflows and the program crashes. On another
|
---|
114 | platform, with a different configuration, or with a different
|
---|
115 | function, the limit might be bigger or smaller.
|
---|
116 | </p>
|
---|
117 | <hr>
|
---|
118 | <div class="header">
|
---|
119 | <p>
|
---|
120 | Next: <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> [<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>
|
---|