
Understanding Memory Management in Computer Programs
Explore the concept of stack memory in computer programs and how the return location is managed. Discover the significance of storing statements in memory during function calls. Dive into the inner workings of memory management in programming.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
Stack Memory 1 (also called Call Stack) Yung-Hsiang Lu Purdue University 1
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) { int a; a = x + 3; f2(a); ... } location 2 location 3 location 4 location 5 location 6 location 7 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 This is a simplified model but it is sufficient here. void f1(int x) { int a; a = f2(x); x = a + 5; ... } location 2 This is the return location of the function call location 3 location 4 location 5 location 6 location 7 3
Return Location in Stack Memory void f1(int x) { int a; a = f2(x); x = a + 5; ... } location 1 location 2 This is the return location of the function call location 3 location 4 location 5 location 6 location 7 location 5 4
Return Location in Stack Memory void f1(int x) { int a; a = f2(x); x = a + 5; ... } int f2(int y) { ... f3( ... ); ... location 1 location 2 This is the return location of the function call f2 location 3 location 4 location 5 location 6 location 7 location 8 location 9 This is the return location of the function call f3 location 12 location 5 location 10 location 11 location 12 5
Return Location in Stack Memory void f1(int x) { int a; a = f2(x); x = a + 5; ... } int f2(int y) { ... f3( ... ); ... location 1 location 2 This is the return location of the function call f2 location 3 location 4 location 5 location 6 location 7 location 8 location 9 location 5 location 10 location 11 location 12 6
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. 7
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. 1 void f1(int x) 2 { int a; a = f2(x); x = a + 5; ... 7 } 8 int f2(int y) 9 { ... f3( ... ); ... 3 4 5 6 line 5 10 11 (Actually, it is called the "program counter", not the line number. We use the line number for simplicity.) 12 8
Push and Pop Push Pop Push: adding an item to the top of the stack Pop: removing the top item from the stack 9
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 { int a; if (not true) { 3 4 5 a = f2(x); .... 6 7 8 10
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 { int a; a = f2 (x); .... a = f2(x); .... 3 4 5 6 7 8 11
The stack memory can store four types of information. The return location is the first type. 12