1

I am trying to understand more about stack overflow, I keep getting descriptions about what it is but I want to understand the causes of it, hope you can help

unwind
  • 364,555
  • 61
  • 449
  • 578
user2160949
  • 131
  • 2
  • 9
  • @Navnath it was an actual programming question about stack overflows, not about the website :) – Marc Claesen May 22 '13 at 11:16
  • 10
    `void foo() { foo(); }` – Alex F May 22 '13 at 11:17
  • We are happy to help you with specific problems! Why don't you read about it (http://en.wikipedia.org/wiki/Stack_overflow) and come back and ask when you have a specific, narrow problem? As it stands, this question is a bit wide to be constructively answered here :) – Magnus Hoff May 22 '13 at 11:17
  • 1
    See http://stackoverflow.com/questions/26158/how-does-a-stack-overflow-occur-and-how-do-you-prevent-it & http://stackoverflow.com/questions/106298/what-is-causing-a-stack-overflow – devnull May 22 '13 at 11:18
  • 1
    Similar question: http://stackoverflow.com/questions/1858053/when-does-the-stack-really-overflow – BenC May 22 '13 at 11:18
  • Deep recursion is one popular cause of stack overflows, as pointed out by @AlexFarber. – Marc Claesen May 22 '13 at 11:18
  • 2
    recursive, n. See `recursive` – devnull May 22 '13 at 11:19
  • See also: http://stackoverflow.com/q/16690326/78845#comment24019998_16690326 – Johnsyweb May 22 '13 at 11:31

2 Answers2

4

Declaring an array on the stack with too many elements can do it, too:

int tmp[999999999];
...
justin
  • 101,751
  • 13
  • 172
  • 222
0

One example is a recursion. C++ doesn't have tail call optimization, so sometimes algorithms working well in functional languages, such as Erlang or Haskell, might lead to stack overflow in C++.

UPD. Though it's not guaranteed by the standart, seems like most of modern compiler implementations do support TCO. Well, you can still reach stack overflow with deep enough recursive function by just declaring any local variables in it.

akalenuk
  • 3,715
  • 4
  • 31
  • 54