When does Stack (Process Stack) Overflow? What are its remedies ?

Stack overflow generally occurs when computer program tries to use more memory space than call stack has available. The call stack is consists of limited amount of address space, often determined at the start of the program. The size of call stack depends on many factors like architecture of the machine on which program runs, programming language, and amount of memory available in the system. When a program tries to use more memory than that is available to call stack i.e. out of call stack bounds, then the stack is said to overflow. Whenever stack overflow occurs due to excessive demand of memory, then program (sometimes the whole computer) may crash.

Reasons for Stack Overflow:
1. This error can be caused by certain types of malware. It can be minimized by reviewing the latest updated OS and avoiding websites and embedded email links.

2. Most common cause is excessively deep or infinite recursion.
Lets understand it with the below code snippet of Infinite recursion in C:
int foo()
{
        return foo();
}

3. Other possible cause is declaring of very large stack variables.

Code snippet in C:
int foo()
{
       double x[1000];
}

Remedies for Stack Overflow:
1. Disallow recursion and cycles
2. Reduce your local variable storage
3. Avoid or strictly limit recursion
4. Keep your call tree shallow

Comments

Popular posts from this blog

Three mislabeled Jar

Difference between Macro And function