0% found this document useful (0 votes)
7 views

CH02

Uploaded by

mastan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

CH02

Uploaded by

mastan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 83

Intermediate C Programming (2nd)

Yung-Hsiang Lu, Purdue University


Geroge K. Thiruvathukal, Loyola University Chicago

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."

• C does not initialize variables, i.e., uninitialized variables contain unknown


values (not zero).

• C does not release memory, i.e., no garbage collection like Java.

• C does not check errors. Programmers are responsible for correctness.

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.

• "Memory" in computers is the reference books and scratch paper

• Computers were very expensive and memory was precious. Thus, memory
management is an important topic when writing computer programs

• Memory management is still important because sizes of data grow fast.

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

User Space Kernel Space

(can be used by ordinary (only operating system


programs) can use)

7
User Space Memory

Stack Memory

Heap Memory

Program Memory

8
User Space Memory
Follow "first-in last-
Stack Memory out" rule

Can be allocated and


Heap Memory released by programs

Program Memory Store programs

9
Stack Memory
• is a type of memory inside computers

• strictly follows the "first-in last-out" (or last-in first-out) rule

• is indirectly controlled by your programs

• is directly controlled by compilers and operating systems

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
}

f2 is popped from f2()


{
f1
the top of the stack
f3();

}
16
Stack Memory 1
(also called Call Stack)

17
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 }
18
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)
This is the return
This is a location 2 {
location of the
simplified model location 3 int a; function call
but it is sufficient location 4 a = f2(x);
location 5 x = a + 5;
here.
location 6 ...
location 7 }
19
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
location 5 x = a + 5;
location 6 ...
location 7 }

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.

This isalways true.


There is no exception in any case.
23
Let's Be More Precise
1 void f1(int x)
Imagine that every statement has a
2 {
corresponding line number.
3 int a;
4 a = f2(x);
When a function is called, the line number
5 x = a + 5;
after the call is stored in the stack memory.
6 ...
7 }
8 int f2(int y)
line 5 9 {
10 ...
(Actually, it is called the "program counter", 11 f3( ... );
not the line number. We use the line number 12 ...
for simplicity.) 24
Push and Pop

Push Pop

• Push: adding an item to the top of the stack


• Pop: removing the top item from the stack

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.

• The function has no static variable.

• The program has no global variable.

• Read "Global Variables Considered Harmful" by Wulf in 1973.

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.

The addresses are always distinct.

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

Two different pieces of information have different addresses, guaranteed by


operating systems

A programmer has no control of the address given by the operating system

"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

Computers Know Only Addresses and Values

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.

Text files cannot run. Compilation coverts text to executable.

Compilation
+ Linking
Human

Source Code Executable


Text File,
Readable
Text Editor 49
gcc: GNU C Compiler

50
gcc: GNU C Compiler

source code

51
gcc -o output

output file name

52
gcc -o output

output file name


Do not call it test because
test is a Linux command.

If you call it test, which program


do you actually execute?

53
Execute the program

execute the program


./ means this directory

54
Print 5 x 5 multiplication

55
Compare correct and wrong answers

correct wrong

56
Enable gcc warnings

-Wall enables warnings


gcc warnings can help you identify problems early.

57
General Rule in Programming:

The earlier you can identify problems, the better.

Do not wait until testing. It requires much more effort.

58
gdb: interactive debugging
• breakpoint: stop at specific line (can be conditional)

• print: see the value of a variable

• see stack memory

59
60
-g after gcc enables debugging

gdb the executable (not .c)

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

r: run the program

list: list the code

66
Set breakpoint by file name: line number

67
Continue

68
print the value

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

• c: continue to the next breakpoint

• bt: show call stack

• b: set a breakpoint

• r: run the program

• list: list the code

75
test coverage

enable test coverage

76
test coverage

execute the program

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy