views:

969

answers:

6

What is a stack overflow error? What type of programs/programming languages is it likely to occur in? Is it unlikely to occur in web application code?

+3  A: 

From wikipedia, of couse:

In software, a stack overflow occurs when too much memory is used on the call stack. In many programming languages, the call stack contains a limited amount of memory, usually 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 too much memory is used on the call stack the stack is said to overflow; typically resulting in a program crash.[1] This class of software bug is usually caused by one of two types of programming errors

FerranB
+1 Kinda leaves you hanging though...
steamer25
This is not answer i am looking
A: 

ha, your english is a bit hard to understand, but i think i get what your asking.

a stack overflow occurs when you are using a stack (duh...) and there is a memory allocation/reading problem. in "web programs", as you put it (i assume you are talking about HTML, PHP, JS), either you don't use stacks or the language used does not allow for low level memory control which prevents these problems.

twolfe18
Lack of control of memory allocation does **not** prevent from stack overflows.
0xA3
Just about every language has a call stack, it is needed so that the code can return to where it was after a subroutines ends. This call stack typically has a fixed size, so after calling too many subroutines without returning, the stack becomes full and will overflow.
Kevin Panko
+16  A: 
victor hugo
From what I understand, this also doesn't happen (at least not as much, i'm not really sure) in modern languages with garbage collection.
thebrokencube
Of course it can happen in a script language. They have a call stack, and that can natually overflow.
Guffa
Yeah it can happen in Java, for example when using a very deep recursion http://stackoverflow.com/questions/860550/stack-overflows-from-deep-recursion-in-java
victor hugo
@thebrokencube: write a recursive function with no good exit condition and you have yourself a stack overflow in any given language, I would say, garbage collected or not.
Fredrik Mörk
@thebrokencube: The stack doesn't have anything to do with garbage collection. Modern applications have a larger stack, which makes it slightly less likely to get a stack overflow, but other than that there is no difference.
Guffa
garbage collection has nothing to do with it. If you have a call stack, then it can become too deep.
Ken Liu
Suggestion: This answer could be improved by also explaining what the stack is in plain English.
Nathan Long
@Nathan I just did
victor hugo
@Fredrik Mörk - well, unless it's tail recursive and your language does tail call optimization. ;)
Edward Kmett
+11  A: 

The stack contains a number of stack frames and is stored in memory. Every time a function is called, a new stack frame is added to the stack. A stack frame contains the arguments to be passed to the function being called, and the return address, so that when the called function has finished the cpu knows where to return to so it can continue executing the calling function. The stack frame may also contain memory to be used by local variables of the function being called.

In this example, the Main function called WriteCustomerDetails and that called PrintToConsole to write out individual bits of data that the WriteCustomerDetails function looked up:

'=======top of stack====================='
Function: PrintToConsole
Arg: John Smith, 34 Acacia Avenue, Age 23
'-----------------------------------------------------------'
Function: WriteCustomerDetails
Arg: John Smith
'-----------------------------------------------------------'
Function: Main
'======bottom of stack==================='

A stack overflow occurs if enough space for the stack was not reserved. Usually a stack sits in one large contiguous block of memory, so isn't divided into chunks, this means one big piece of memory is needed for it, and this makes it hard for the runtime to try and grow the space reserved for the stack if it fills up.

A stack-overflow can often occur when a function is accidentally written that calls itself. Sometimes it's ok for a function to call itself as long as there is an 'if' or some condition in the function that stops the calls at some point. This is called a recursive function. But, if there is no stopping and the function keeps calling itself, or maybe two or more functions keep calling each other, then very quickly they will eat all of the stack memory up. When there's none left, you get a stack-overflow and the program crashes.

It is possible for this to happen in any program, they don't necessarily have to be complex, and it can happen in code running a website. And, it can occur in scripting languages too.

Scott Langham
Very nice answer, +1
0xA3
+1 Thanks for writing this up Scott.
James McMahon
Very nice explanation. Clear and easily understandable.
Nirmal
+5  A: 

A stack overflow happens when you use too much stack space. There is generally two situations when this happens:

The first is when you have an error in the code, causing a recursive loop without an exit. For example a property reading from itself:

public int Length {
   get {
      return Length;
   }
}

The second is when you have a recursive loop that is too deep. As the stack space is limited, you can only nest an algorithm a certain number of times. If your algorithm is nested too deep so that it runs out of stack space before it exists, you get a stack overflow. Example:

public bool Odd(int value) {
   if (value == 0) {
      return false;
   } else {
      return !Odd(value - 1);
   }
}

If you call this method with a too large value, it will nest too deep and cause a stack overflow.

Guffa
The reason for the stack overflow in both of your examples is recursion. However, there is indeed another quite simple reason: If a (local) variable or function argument that is allocated on the stack is too large, typically this would happen with arrays, see http://en.wikipedia.org/wiki/Stack_overflow
0xA3