Prerequisite : Memory Layout of C Programs
In a typical process, a stack segment of program contains local variables along with information that is saved each time a function is called. Each time a function is called, the address of where to return to and certain information about the caller’s environment, such as some of the machine registers, are saved on the stack. The newly called function then allocates room on the stack for its automatic and temporary variables.
Stack may grow downward or upward depending on environment for which code is compiled, i.e., depends on compiler. How to know whether stack grows downward or upward?
The idea is to do following :
1) Make a local variable in main.
2) Write another function fun() with its own local variable.
3) Make a call to fun() from main. Compare addresses of two local variable. If address of fun()’s local variable is more than main’s local variable, then stack grows upward (i.e., addresses increase)
This article is contributed by Akash Sharan. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Get the stack size and set the stack size of thread attribute in C
- Heap overflow and Stack overflow
- Inter-process Communication using a shared stack
- Stack vs Heap Memory Allocation
- C program to detect tokens in a C program
- C program to print a string without any quote (singe or double) in the program
- Hello World Program : First program while learning Programming
- C Program to find IP Address, Subnet Mask & Default Gateway
- 8085 program to find the element that appears once
- 8085 program to find maximum and minimum of 10 numbers
- 8085 program to find maximum of two 8 bit numbers
- Assembly language program to find largest number in an array
- 8085 program to find the factorial of a number
- 8085 program to find square root of a number
- 8085 program to find 1's and 2's complement of 8-bit number
- 8085 program to find 1’s and 2’s complement of 16-bit number
- 8085 program to find sum of digits of 8 bit number
- 8085 program to find nth power of a number
- 8085 program to find square of a 8 bit number
- 8086 program to find the factorial of a number