CH02
CH02
CRC Press 1
ISBN 9781032189819
Chapter 02 Stack Memory
2
Memory in Computer
3
C vs. Newer Languages
• When C was developed (late 1960s to early 1970s), computers were slow.
Thus, C's design principle is "A C program does exactly what it is instructed to,
nothing more."
4
Why Do Computers Need Memory?
• Imagine that you are solving a complex problem. You need some reference
books (also called "read-only memory") and some scratch paper.
• Computers were very expensive and memory was precious. Thus, memory
management is an important topic when writing computer programs
5
This core memory is about the size of your mobile phone.
Your phone can store about 10,000,000 times more data.
6
Memory in Computers
7
User Space Memory
Stack Memory
Heap Memory
Program Memory
8
User Space Memory
Follow "first-in last-
Stack Memory out" rule
9
Stack Memory
• is a type of memory inside computers
10
What is stack?
• "Stack" means what comes first leaves last.
• You are using this concept everyday.
• You put on socks before putting on shoes. You take off the shoes before taking
off the socks.
• You put on a shirt before wearing a jacket. You take off the jacket before taking
off the shirt.
• When you put a book on the top of a pile, the last added book is removed first.
11
Stack Memory in Programs
Consider this snippet of code: f1 calls f2
and f2 calls f3 f1()
{
f2();
}
f1 is seen first, then f2, then f3
f2()
{
f3();
}
When f3 finishes, the program continues
from f2, not f1
12
Stack Memory in Programs
The stack memory has only one
function, i.e., f1 f1()
{
the program is here
f2();
f2()
{
f3();
f1
}
13
Stack Memory in Programs
Conventionally, the stack memory starts
from bottom and grows upwards. f1()
{
f2();
The stack memory has two functions
}
now, i.e., f1 and f2
f2 is pushed to f2()
{
f2
the top of the stack
the program is here f3();
f1
}
14
Stack Memory in Programs
After f3 finishes, the program continues
f2. f1()
{
f2();
f2()
{
f2
f3();
f1 the program continues here
}
15
Stack Memory in Programs
After f2 finishes, the program continues
f1. f1()
{
f2();
the program continues here
}
}
16
Stack Memory 1
(also called Call Stack)
17
Return Location
Computer programs are stored in memory. Every
statement has a location in memory.
location 5
20
Return Location in Stack Memory
location 1 void f1(int x)
location 2 {
location 3 int a; This is the return
location 4 a = f2(x); location of the
function call f2
location 5 x = a + 5;
location 6 ...
location 7 }
location 8 int f2(int y)
location 9 {
location 10 ... This is the return
location 12 location of the
location 11 f3( ... ); function call f3
location 5
location 12 ... 21
Return Location in Stack Memory
location 1 void f1(int x)
location 2 {
This is the return
location 3 int a;
location of the
location 4 a = f2(x); function call f2
location 5 x = a + 5;
location 6 ...
location 7 }
location 8 int f2(int y)
location 9 {
location 10 ...
location 5
location 11 f3( ... );
location 12 ... 22
When a function is called, the
statement after the call is stored in
the stack memory.
Push Pop
25
This is a run-time action
1 void f1(int x)
Nothing is pushed if there is no function call
2 {
during execution
3 int a;
4 if (not true)
In this example, function f2 is not called and
5 {
the return location (line 7) is not pushed to
6 a = f2(x);
the stack memory
7 ....
8
26
Different Call Sites
1 void f1(int x)
In this example, function f2 is called in two
2 {
different locations (lines 4 and 6). Each call
3 int a;
has corresponding return location (line 5 and 4 a = f2 (x);
7 respectively) 5 ....
6 a = f2(x);
7 ....
8
27
The stack memory can store four
types of information. The return
location is the first type.
28
Stack Memory 2
(also called Call Stack)
29
When a function is called, the line
number after the call is stored in the
stack memory.
30
Arguments in Function Call
1 void f1(int x)
When a function is called with one
2 {
(or several) argument, the
3 int a;
argument is stored (pushed) to the 4 a = f2(7);
stack memory. 5 x = a + 5;
6 ...
7 }
8 int f2(int y)
9 {
y=7 10 ...
line 5
31
Arguments in Function Call
1 void f1(int x)
When a function is called with one
2 {
(or several) argument, the
3 int a;
argument is stored (pushed) to the 4 a = f3(7, 3.2);
stack memory. 5 x = a + 5;
6 ...
7 }
8 int f3(int y,
double z)
z = 3.2 {
y=7 10 ...
line 5
32
How to write good code?
• Make each function's behavior depends on only the input arguments and
nothing else.
33
When a function is called, the statement after the
call is stored in the stack memory
If the call has argument (or arguments), the
arguments are stored in the stack memory.
34
The frame of a called function
1 void f1(int x)
The return location and the
2 {
arguments' values form a "frame"
3 int a;
of the called function. 4 a = f3(7, 3.2);
5 x = a + 5;
6 ...
7 }
z = 3.2 8 int f3(int y,
Frame of f3 double z)
y=7
9 {
line 5 10 ...
35
Local Variables
1 void f1(int x)
A function's local variables are
2 {
also stored in the frame.
3 int a;
4 a = f3(7, 3.2);
If a function has several local
5 x = a + 5;
variables, all of them are stored in
6 ...
the frame.
7 }
8 int f3(int y,
b = 10 double z)
z = 3.2 9 {
Frame of f3
y=7 10 int b = y + 3;
line 5
36
Stack Memory 3
37
Memory is "address-value" pairs
Memory is organized into rows. Each row has an address and stores a value.
You can think of the addresses in memory as home addresses and the values are
the names of the families living at that address
Address Value
001 C Language
002 Year 2016
Taylor
Smith 003 5
Johnson
004 36.97
38
Some Rules about Addresses
Addresses are assigned by operating systems
"Zero" is never a valid address. C programs has a special symbol (NULL) for this
address.
39
Value Address
1 void f1(int x)
2 {
3 int a;
106 z = 3.2 4 a = f3(7, 3.2);
Frame of f3 105 y=7 5 ...
104 VA 102 6 ...
7 }
103 line 5
8 int f3(int y,
double z)
102 a=
9 {
Frame of f1 101 x= 10 ...
100 line ? 11 return ...
40
Memory Organization
1 void f1(int x)
Frame Symbol Address Value
2 {
z 106 3.2
3 int a;
f3 y 105 7 4 a = f3(7, 3.2);
VA 104 102 5 ...
RL 103 line 5 6 ...
a 102 7 }
f1 x 101 8 int f3(int y,
double z)
RL 100 line ? 9 {
RL: return location 10 ...
VA: value address 11 return ...
41
Frames and Symbols for Humans
42
Memory Organization
For Humans
1 void f1(int x)
Frame Symbol Address Value
2 {
z 106 3.2
3 int a;
f3 y 105 7 4 a = f3(7, 3.2);
VA 104 102 5 ...
RL 103 line 5 6 ...
a 102 7 }
f1 x 101 8 int f3(int y,
double z)
RL 100 line ? 9 {
RL: return location 10 ...
VA: value address 11 return ...
43
1. return locations
2. arguments
3. local variables
4. value addresses
44
Linux Tools for C Programming
45
Topics
• programming tools in Linux
• gcc
• gdb
• gcov
46
Many Linux tools for C Programming
47
A Simple C Program
48
Compilation
Humans write text files but computers do not understand text.
Compilation
+ Linking
Human
50
gcc: GNU C Compiler
source code
51
gcc -o output
52
gcc -o output
53
Execute the program
54
Print 5 x 5 multiplication
55
Compare correct and wrong answers
correct wrong
56
Enable gcc warnings
57
General Rule in Programming:
58
gdb: interactive debugging
• breakpoint: stop at specific line (can be conditional)
59
60
-g after gcc enables debugging
61
Set breakpoint by the name of a function
62
Set breakpoint by the line number
63
Run the program with two arguments 3 and 5
64
list code around the breakpoint
65
gdb commands
b: set a breakpoint
66
Set breakpoint by file name: line number
67
Continue
68
print the value
69
backtrace, show call stack
70
currently running function is frame 0
71
line number
72
continue
73
shows three frames
74
gdb commands
• print: print a variable
• b: set a breakpoint
75
test coverage
76
test coverage
77
test coverage
run gcov
78
test coverage
generated files
79
# means untested
80
different test inputs
81
# means untested 82
2: Some lines have been tested twice. 83