Download presentation
Presentation is loading. Please wait.
1
Stack Memory 1 (also called Call Stack)
Yung-Hsiang Lu Purdue University
2
Return Location Computer programs are stored in memory. Every statement has a location in memory. When a function is called, the location of the statement after this function call is stored in the stack memory. location 1 void f1(int x) location 2 { location 3 int a; location 4 a = x + 3; location 5 f2(a); location 6 ... location 7 }
3
Return Location Computer programs are stored in memory. Every statement has a location in memory. When a function is called, the location of the statement after this function call is stored in the stack memory. location 1 void f1(int x) location 2 { location 3 int a; location 4 a = f2(x); location 5 x = a + 5; location 6 ... location 7 } This is a simplified model but it is sufficient here. This is the return location of the function call
4
Return Location in Stack Memory
void f1(int x) location 2 { location 3 int a; location 4 a = f2(x); location 5 x = a + 5; location 6 ... location 7 } This is the return location of the function call location 5
5
Return Location in Stack Memory
void f1(int x) location 2 { location 3 int a; location 4 a = f2(x); location 5 x = a + 5; location 6 ... location 7 } location 8 int f2(int y) location 9 location 10 location 11 f3( ... ); location 12 This is the return location of the function call f2 This is the return location of the function call f3 location 12 location 5
6
Return Location in Stack Memory
void f1(int x) location 2 { location 3 int a; location 4 a = f2(x); location 5 x = a + 5; location 6 ... location 7 } location 8 int f2(int y) location 9 location 10 location 11 f3( ... ); location 12 This is the return location of the function call f2 location 5
7
This is always true. There is no exception in any case.
When a function is called, the statement after the call is stored in the stack memory. This is always true. There is no exception in any case.
8
Let's Be More Precise Imagine that every statement has a corresponding line number. When a function is called, the line number after the call is stored in the stack memory. (Actually, it is called the "program counter", not the line number. We use the line number for simplicity.) 1 void f1(int x) 2 { 3 int a; 4 a = f2(x); 5 x = a + 5; 6 ... 7 } 8 int f2(int y) 9 10 11 f3( ... ); 12 line 5
9
Push and Pop Push Pop Push: adding an item to the top of the stack
Pop: removing the top item from the stack
10
This is a run-time action
Nothing is pushed if there is no function call during execution In this example, function f2 is not called and the return location (line 7) is not pushed to the stack memory 1 void f1(int x) 2 { 3 int a; 4 if (not true) 5 6 a = f2(x); 7 .... 8
11
Different Call Sites In this example, function f2 is called in two different locations (lines 4 and 6). Each call has corresponding return location (line 5 and 7 respectively) 1 void f1(int x) 2 { 3 int a; 4 a = f2 (x); 5 .... 6 a = f2(x); 7 8
12
The stack memory can store four types of information
The stack memory can store four types of information. The return location is the first type.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.