Its main use is to store local variables, arguments and return address each time a function is called.
When your program calls a function the following happen :
- The function arguments are put on the stack
- The current instruction pointer is put on the stack
- The program jumps to the start of the function
- Space is allocated on the stack to hold local variables
- The function executes
- The space holding local variables is de-allocated
- The instruction pointer is restored and removed from the stack (we are now leaving the function and resuming the calling procedure)
- The arguments are removed from the stack
Because the stack pointer marks the top of the stack. If it is not initialised, it is not possible to determine where the next stack frame will go.
what ever you assume
Both of them are pointers, but otherwise they are completely unrelated. The former points to the current position of the stack, the latter points to the current instruction of the program.
A stack is implicitly sorted by hierarchical nested order. It does not make sense to sort a stack. Do you mean a list? If so, please ask the question again.
Two little problems: 1. stack doesn't have a flow-chart 2. there are no flow-charts in a C program
The stack pointer keeps track of the top of the stack used by the current thread. The program counter keeps track of the next instruction in a program. Both are registers and both store a memory address.
The Program Counter and Stack Pointer registers are called special purpose registers because they can not be used arbitrarily; they are, well, special. You cannot, for instance, load a new value into the Program Counter and blithely continue, that action would cause an unconditional jump. Also, the Program Counter is automatically incremented by the size of the instruction, so you can not just put something in it and expect its value to persist. Similarly, you cannot load a new value into the Stack Pointer without losing the entire stack context that you are in.
A stack overflow is a type of buffer overflow in which an array writes memory outside of the array boundaries. The keyword here is "stack". The stack is a section in memory in which local variables and other program data are kept for future reference. When the stack gets overflown, adjacent program memory, such as variables, pointers, etc, will be overwritten and cause your program to crash.
Because the stack pointer marks the top of the stack. If it is not initialised, it is not possible to determine where the next stack frame will go.
Stack implementations allow us to easily implement backtracking algorithms.
int top=-1; int stack[10];
On the stack.
stack register is a special purpose register.
what ever you assume
Both of them are pointers, but otherwise they are completely unrelated. The former points to the current position of the stack, the latter points to the current instruction of the program.
stack abstract datatype
Yes, but not for long term storage, only while a program is executing using its stack.