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?
相关问题
- How can I increase the maximum call stack size in
- Name for a method that has only side effects
- Ada beginner Stack program
- condensed version of console.trace()
- Segmentation fault in C
相关文章
- Threading in C# , value types and reference types
- Should client-server code be written in one “proje
- Algorithm for maximizing coverage of rectangular a
- Stack<> implementation in C#
- Is there an existing solution for these particular
- Java, Printing the stack values
- What is Scope Creep? [closed]
- Is there a stack space for every thread?
From Wikipedia:
The stack is a data structure that keeps record of the point the subroutines of a program should return control to when they finish executing. The return addresses are pushed in the stack as the subroutines are being invoked, when the subroutine finish its execution the return address is pulled from the stack. If there are many subroutines and there is no space in the stack a stack overflow happens.
Also in the stack is intended to store local variables so if a local variable is too large is more probable the stack doesn't have space to store it, if this is the case a stack overflow happens too.
Wikipedia includes a nice diagram picturing the stack when a
DrawLine
subroutine is called from another subroutine calledDrawSquare
, I hope this picture helps to understand better the stack structure.There are two main causes of a stack overflow: deep function recursions and excessively large stack variables. Since these are common terms in almost all programming languages a stack overflow can happen besides the complexity of the language.
Guffa contribution: 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.
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:
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:
If you call this method with a too large value, it will nest too deep and cause a stack overflow.
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.
From wikipedia, of couse:
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.