Does stack grow upward or downward? Have you faced this question? Many freshers and beginners ask this question. Also, some interviewer asks the question that finds the direction of growth of stack. We can find the direction of growth of the stack using a simple C program. But before starting the explaining the program, I want to explain few points related to the stack growth direction.
The stack growth doesn’t usually depend on the operating system itself, but on the processor, it’s running on. Also, the stack for different architectures can grow either way (downward or upward), but for architecture, it will be consistent.
The stack’s growth (growing up or growing down) decided by the ABI (Application binary interface) of that architecture and how the call stack (activation record) is organized. Let’s see some processors and their direction:
x86: down. SPARC: selectable. The standard ABI uses down. ARM: selectable, but Thumb2 has compact encodings only for down (LDMIA = increment after, STMDB = decrement before).
C Program to find direction of growth of stack:
As we know stack can grow up or down, so let’s create logic to find the stack is growing upward or downward.
- Create a local variable in the main function.
- Now create a function ‘fun()’ with a local variable.
- Call the function ‘fun()’ from the main function. And Compare addresses of two local variables.
- If the address of fun()’s local variable is more than the main’s local variable, then the stack grows upward otherwise it grows downward.
// C program to check whether stack grows // downward or upward. #include<stdio.h> void fun(int *main_local_var_addr) { //local variable of fun function int fun_local_var; if (&fun_local_var > main_local_var_addr) { printf("Stack grows upward\n"); } else { printf("Stack grows downward\n"); } } int main() { //local variable of main function int main_local_var; //calling the fun fun(&main_local_var); return 0; }
Output:
Explanation of the code:
As we know that in the mentioned C program the ‘main’ function is calling to the ‘fun’ function. So the stack frame as seen by ‘fun’ is as follows (taking a reference to explain the code):
direction of | | growth of +---------------------------------+ stack |Parameters passed by main(caller)| from higher addr.| | to lower addr. | | | +---------------------------------+ <-- SP on entry to fun() | | Return address | V +---------------------------------+ | Old BP | +---------------------------------+ | Callee saved registers being | | used in the callee function | +---------------------------------+ | Local variables of fun | |(Direction of growth of frame is | | same as direction of growth of | | stack) | +---------------------------------+ | Arguments to functions called | | by fun | +---------------------------------+ <- Current SP after stack frame is allocated
Now you can see the stack grows downward. So, if the variables are allocated to the local frame of the ‘fun’ function (fun_local_var), the variable’s addresses actually grow downward.
You can see line number 10, where I am comparing the addresses of local variables of main() and fun() functions.
//Comparing the address if (&fun_local_var > main_local_var_addr)
Because in my case stack is growing downward so the ‘fun’ function local variable address (&fun_local_var) will be less than the address of the main function local variable (&main_local_var_addr). Also, I am printing a message using the print function which helps the programmer to find that stack is growing upward or downward.
Recommended Posts for you
- Memory Layout of C program.
- Internal, external and none linkage in C.
- Solved Programming Language MCQs
- List of Gifts for the programmers and techies.
- Memory Leak in C/C++ programming.
- C interview questions, you should check.
- C++ interview questions with answers.
- Create an employee management system in C.
- Top 11 Structure Padding Interview Questions in C
- structure in C: you should know in depth
- What is flexible array member in c?
- What is importance of struct hack in c?