The stack is a place in the computer memory where all the variables that are declared and initialized before runtime are stored.
A stack is a last in, first out (LIFO) data structure. The push operation adds to the top of the stack, hiding any items already on the stack, or initializing the stack if it is empty. The pop operation removes an item from the top of the stack, and returns this value to the caller. A pop either reveals previously concealed items, or results in an empty stack.
Stack is used in
1. Expression evaluation and syntax parsing
Many compilers use a stack Expression evaluation before translating into low level code.
Example (general)
The calculation: 1 + 2 * 4 + 3 can be written down like this in postfix notation with the advantage of no precedence rules and parentheses needed:
The expression is evaluated from the left to right using a stack:
1. when encountering an operand: push it
2. when encountering an operator: pop two operands, evaluate the result and push it. Like the following way (the Stack is displayed after Operation has taken place):
Input | Operation | Stack (after op) |
1 | Push operand | 1 |
2 | Push operand | 2, 1 |
4 | Push operand | 4, 2, 1 |
* | Multiply | 8, 1 |
+ | Add | 9 |
3 | Push operand | 3, 9 |
+ | Add | 12 |
The final result, 12, lies on the top of the stack at the end of the calculation.
#include<stdio.h>
int main()
{
int a[100], i;
printf("To pop enter -1\n");
for(i = 0;;)
{
printf("Push ");
scanf("%d", &a[i]);
if(a[i] == -1)
{
if(i == 0)
{
printf("Underflow\n");
}
else
{
printf("pop = %d\n", a[--i]);
}
}
else
{
i++;
}
}
}
2. Runtime memory management
Almost all computer runtime memory environments use a special stack (the "call stack") to hold information about procedure/function calling and nesting in order to switch to the context of the called function and restore to the caller function when the calling finishes. They follow a runtime protocol between caller and callee to save arguments and return value on the stack. Stacks are an important way of supporting nested or recursive function calls. This type of stack is used implicitly by the compiler to support CALL and RETURN statements (or their equivalents) and is not manipulated directly by the programmer.
Some programming languages use the stack to store data that is local to a procedure. Space for local data items is allocated from the stack when the procedure is entered, and is deallocated when the procedure exits. The C programming language is typically implemented in this way
Stack Overflow:-
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.[1] This class of software bug is usually caused by one of two types of programming errors.[2]
Infinite recursion: The most common cause of stack overflows is excessively deep or infinite recursion
2.Very large stack variables: This is usually the result of creating local array variables that are far too large. For this reason arrays larger than a few kilobytes should be allocated dynamically instead of as a local variable.[4]
what is the stack where it is used ? what is the use of the stack