tags:

views:

259

answers:

6

Possible Duplicate:
What is a stack overflow error?

What is a stack overflow?

+2  A: 

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

Svisstack
Smarty pants ;)
spender
+4  A: 

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

From the Wikipedia entry for a stackoverflow

John Sibly
+2  A: 

A stack overflow is an error condition that results from the required stack space growing beyond the maximum amount of memory available to the stack. This is usually down to recursion without suitable temination cases, but could result from (say) using alloca() to allocate stack memory.

Vatine
+2  A: 

When one method calls another method, which calls another method, which calls another method, you end up having a call stack that keeps track of what method to return to, when the current method returns.

The call stack can be pretty extensive. If you get a stack overflow, you're likely to have invoked a method recursively an infinite amount of times.

public String A() {
    return "A" + A();
}
David Hedlund
A: 

Most systems have the concept of "the stack" as a separate memory area which is used "last in, first out" for function-local stuff like parameters and local variables. For every function call, its data is pused on top of the stack and when the call finishes, that data is popped off the stack leaving the calling function with the return value and its own (unchanged) local data. This allows functions to call themselves (recursion).

However, there is only a limited amount of memory available for the stack. If your call hierarchy is too deep, it runs out and that's a stack overflow.

Typically, this happens when the programmer makes a mistake and creates an unbounded recursion. But it can also happen when you simply put too much data on the stack, such as allocating multi-megabayte arrays locally.

Michael Borgwardt
A: 

We should begin by explaining what is a program stack.

When a program need memory to store return point for function calls, of local variables, it need a really simple and fast memory allocator, as it is doing this all the time. With linear programming models a good structure to do that is to use a stack. When entering a function the program reserve some space in the stack, when exiting function it frees it, etc. There is usually such a stack for every control flow in the program (one for each thread or processes).

This structure is so efficient and common that current processors have assembly instructions dedicated for stack management, including the equivalent of function at very low level (call with return).

Now, that memory space have necessarilly a limited size, and if using too large local variables or calling too many function, you can grow outside the stack and access to non reserved space. This can lead to different behaviors depending on hardware and software support, usually it breaks the program that exit with some error.

This is a stack overflow, and a well fitting name for a place where you speak of programming related errors.

As a side note, the stack is not the only possible mechanism to implement function calls with return and local variables and there is also other means (I have in mind CPS, Continuation Passing Style). And system program stacks can become a bootleneck for massive parallelism, but this is another debate.

kriss