Questions tagged [stack-overflow]

NOT THIS WEBSITE! Use this tag for the error caused by pushing too many items onto the callstack. If you have a question regarding this website, please go to https://meta.stackoverflow.com

DO NOT USE THIS TAG TO REFER TO THE STACK OVERFLOW WEBSITE! If you have a question regarding the site, please go to Meta.


In software, a stack overflow occurs if the call stack pointer exceeds the stack bound. The call stack may consist of a limited amount of address space, often determined at the start of the program. The size of the call stack depends on many factors, including the programming language, machine architecture, multi-threading, and amount of available memory. When a program attempts to use more space than is available on the call stack (that is, when it attempts to access memory beyond the call stack's bounds, which is essentially a buffer overflow), the stack is said to overflow, typically resulting in a program crash.

(Wikipedia: Stack overflow)

See How does a “stack overflow” occur and how do you prevent it? for general advice on stack overflow exceptions, what they are, etc.

See also:

4171 questions
78
votes
7 answers

Why is Python recursion so expensive and what can we do about it?

Suppose we want to compute some Fibonacci numbers, modulo 997. For n=500 in C++ we can run #include #include std::array fib(unsigned n) { if (!n) return {1, 1}; auto x = fib(n - 1); return {(x[0] +…
jtallk
  • 961
  • 1
  • 4
  • 6
76
votes
3 answers

Why does a recursive call cause StackOverflow at different stack depths?

I was trying to figure out hands-on how tail calls are handled by the C# compiler. (Answer: They're not. But the 64bit JIT(s) WILL do TCE (tail call elimination). Restrictions apply.) So I wrote a small test using a recursive call that prints how…
Cristian Diaconescu
  • 34,633
  • 32
  • 143
  • 233
75
votes
3 answers

Why doesn't this statement throw a StackOverflowError?

I just saw this weird piece of code in another question. I thought it would result in a StackOverflowError being thrown, but it doesn't... public class Node { private Object one; private Object two; public static Node NIL = new…
Anthony Raymond
  • 7,434
  • 6
  • 42
  • 59
73
votes
10 answers

How do I prevent and/or handle a StackOverflowException?

I would like to either prevent or handle a StackOverflowException that I am getting from a call to the XslCompiledTransform.Transform method within an Xsl Editor I am writing. The problem seems to be that the user can write an Xsl script that is…
JohnnyM
  • 28,758
  • 10
  • 38
  • 37
67
votes
2 answers

F# vs OCaml: Stack overflow

I recently found a presentation about F# for Python programmers, and after watching it, I decided to implement a solution to the "ant puzzle" on my own. There is an ant that can walk around on a planar grid. The ant can move one space at a time…
ttsiodras
  • 10,602
  • 6
  • 55
  • 71
66
votes
6 answers

How is a StackOverflowException detected?

TL;TR When I asked the question I assumed a StackOverflowException is a mechanism to prevent applications to run infinitely. This is not true. A StackOverflowException is not being detected. It is thrown when the stack does not have the capacity to…
Noel Widmer
  • 4,444
  • 9
  • 45
  • 69
65
votes
2 answers

How can I increase the maximum call stack size in Node.js

This is different to other questions regarding an error message in Node that reads RangeError: Maximum call stack size exceeded in that I know exactly why I'm getting this error message. It's happening because I'm recursing, recursing quite a lot in…
thomas-peter
  • 7,738
  • 6
  • 43
  • 58
64
votes
3 answers

Why does this method result in an infinite loop?

One of my coworkers came to me with a question about this method that results in an infinite loop. The actual code is a bit too involved to post here, but essentially the problem boils down to this: private IEnumerable GoNuts(IEnumerable
Jim Mischel
  • 131,090
  • 20
  • 188
  • 351
62
votes
8 answers

Java stack overflow error - how to increase the stack size in Eclipse?

I am running a program that I've written in Java in Eclipse. The program has a very deep level of recursion for very large inputs. For smaller inputs the program runs fine however when large inputs are given, I get the following error: Exception in…
tree-hacker
  • 5,351
  • 9
  • 38
  • 39
61
votes
2 answers

ES6 Tail Recursion Optimisation Stack Overflow

Having read Dr Rauschmayer's description of recursive tail call optimisation in es6, I've since been trying to recreate the 'zero-stack' execution of the recursive factorial function he details. Using the Chrome debugger to step between stack…
61
votes
3 answers

Why does .NET behave so poorly when StackOverflowException is thrown?

I'm aware that StackOverflowExceptions in .NET can't be caught, take down their process, and have no stack trace. This is officially documented on MSDN. However, I'm wondering what the technical (or other) reasons are behind the behavior. All MSDN…
ChaseMedallion
  • 20,860
  • 17
  • 88
  • 152
53
votes
7 answers

Confusing output from infinite recursion within try-catch

Consider the following code. public class Action { private static int i=1; public static void main(String[] args) { try{ System.out.println(i); i++; main(args); }catch (StackOverflowError e){ …
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
49
votes
1 answer

Stackoverflow doing boxing in C#

I have these two chunks of code in C#: First class Program { static Stack S = new Stack(); static int Foo(int n) { if (n == 0) return 0; S.Push(0); S.Push(1); ... S.Push(999); …
lcastillov
  • 2,163
  • 1
  • 11
  • 17
49
votes
6 answers

How to predict the maximum call depth of a recursive method?

For the purposes of estimating the maximum call depth a recursive method may achieve with a given amount of memory, what is the (approximate) formula for calculating the memory used before a stack overflow error is likely to occur? Edit: Many have…
Bohemian
  • 412,405
  • 93
  • 575
  • 722
48
votes
3 answers

java.lang.StackOverflowError while using a RegEx to Parse big strings

This is my Regex ((?:(?:'[^']*')|[^;])*)[;] It tokenizes a string on semicolons. For example, Hello world; I am having a problem; using regex; Result is three strings Hello world I am having a problem using regex But when I use a large input…
Ali
  • 7,810
  • 12
  • 42
  • 65