University of Michigan at Ann Arbor
Last Edit Date: 01/09/2023
Disclaimer and Term of Use:
We do not guarantee the accuracy and completeness of the summary content. Some of the course material may not be included, and some of the content in the summary may not be correct. You should use this file properly and legally. We are not responsible for any results from using this file
This personal note is adapted from Professor Amir Kamil, Andrew DeOrio, James Juett, Sofia Saleem, and Saquib Razak. Please contact us to delete this file if you think your rights have been violated.
This work is licensed under a Creative Commons Attribution 4.0 International License.
In stack-based memory management, activation records are stored in a data structure called a stack.
A stack works just like a stack of pancakes: when a new pancake is made, it is placed on top of the stack, and when a pancake is removed from the stack, it is the top pancake that is taken off. Thus, the last pancake to be made is the first to be eaten, resulting in last-in, first-out (LIFO) behavior.
Activation records are similarly stored: when an activation record is created, it is placed on top of the stack, and the first activation record to be destroyed is the last one that was created.
Take the following code as an example:
1 void bar() { 2 } 3 4 void foo() { 5 bar(); 6 } 7 8 int main() { 9 foo(); 10 }
When the program runs, it begins with main()
function, put main()
in the stack. Then it calls the foo()
function, put foo()
on the top of main()
. After that, the foo()
function calls the bar()
function, put bar()
on the top of foo()
.
When returning values, bar()
function goes first, then foo()
, and finally main()
.
The following picture shows the process.
Stack can also grow downward as the following picture shows.
Here is a more complex example:
1 int plus_one(int x) { 2 return x + 1; 3 } 4 5 int plus_two(int x) { 6 return plus_one(x + 1); 7 } 8 9 int main() { 10 int result = 0; 11 result = plus_one(0); 12 result = plus_two(result); 13 cout << result; // prints 3 14 }
Try it out:
Click here to open in new windowWhen the program runs, it first calls main()
. Initializing int result = 0;
. Then call function plus_one(0)
. After that, plus_one(int x)
return 1
into main()
, so that result = 1
after executing line 11. The process is shown as the following picture.
At line 12, the program calls the function plus_two(in x)
. Since result = 1
at this point, so we have plus_two(1)
. In the plus_two(int x)
function, we call plus_one(x + 1)
function again, which is equivalent to plus_one(2)
. The process is shown as the following picture.
Then plus_one(2)
returns x first (x = 3
), then plus_two(1)
returns x to main()
. Finally main()
assign result = 3
and print it out. The process is shown as the following picture.