Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Stack overflow
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Infinite recursion=== {{Main|Infinite recursion}} The most-common cause of stack overflow is excessively deep or infinite recursion, in which a function calls itself so many times that the space needed to store the variables and information associated with each call is more than can fit on the stack.<ref name="segFault">[https://stackoverflow.com/questions/2685413/what-is-the-difference-between-a-segmentation-fault-and-a-stack-overflow/2685434#2685434 What is the difference between a segmentation fault and a stack overflow?] {{Webarchive|url=https://web.archive.org/web/20210913132615/https://stackoverflow.com/questions/2685413/what-is-the-difference-between-a-segmentation-fault-and-a-stack-overflow/2685434#2685434 |date=2021-09-13 }} at [[Stack Overflow]]</ref> An example of infinite recursion in [[C (programming language)|C]]. <syntaxhighlight lang="c"> int foo() { return foo(); } </syntaxhighlight> The function ''foo'', when it is invoked, continues to invoke itself, allocating additional space on the stack each time, until the stack overflows resulting in a [[segmentation fault]].<ref name="segFault"/> However, some compilers implement [[tail-call optimization]], allowing infinite recursion of a specific sortβ[[tail recursion]]βto occur without stack overflow. This works because tail-recursion calls do not take up additional stack space.<ref name="tailRecur">{{cite web |title = An Introduction to Scheme and its Implementation |url = http://www.federated.com/~jim/schintro-v14/schintro_73.html |date = 1997-02-19 |url-status = dead |archive-url = https://web.archive.org/web/20070810213035/http://www.federated.com/~jim/schintro-v14/schintro_73.html |archive-date = 2007-08-10 }}</ref> Some C compiler options will effectively enable [[tail-call optimization]]; for example, compiling the above simple program using [[GNU Compiler Collection|gcc]] with <code>-O1</code> will result in a segmentation fault, but not when using <code>-O2</code> or <code>-O3</code>, since these optimization levels imply the <code>-foptimize-sibling-calls</code> compiler option.<ref>{{cite web|title=Using the GNU Compiler Collection (GCC): Optimize Options|url=https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html|access-date=2017-08-20|archive-date=2017-08-20|archive-url=https://web.archive.org/web/20170820201527/https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html|url-status=live}}</ref> Other languages, such as [[Scheme (programming language)|Scheme]], require all implementations to include tail-recursion as part of the language standard.<ref>{{cite journal |author = Richard Kelsey |date = August 1998 |title = Revised<sup>5</sup> Report on the Algorithmic Language Scheme |url = http://www.schemers.org/Documents/Standards/R5RS/ |journal = Higher-Order and Symbolic Computation |volume = 11 |issue = 1 |pages = 7β105 |doi = 10.1023/A:1010051815785 |accessdate = 2012-08-09 |author2 = William Clinger |author3 = Jonathan Rees |display-authors = 3 |last4 = Rozas |first4 = G.J. |last5 = Adams Iv |first5 = N.I. |last6 = Friedman |first6 = D.P. |last7 = Kohlbecker |first7 = E. |last8 = Steele Jr. |first8 = G.L. |last9 = Bartley |first9 = D.H. |s2cid = 14069423 |archive-date = 2007-01-05 |archive-url = https://web.archive.org/web/20070105152327/http://www.schemers.org/Documents/Standards/R5RS/ |url-status = live |url-access= subscription }}</ref>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)