Professional Documents
Culture Documents
CIS 190: C/C++ Programming: Assorted Topics (And More On Pointers)
CIS 190: C/C++ Programming: Assorted Topics (And More On Pointers)
Lecture 4
Assorted Topics
(and More on Pointers)
1
Outline
Makefiles
File I/O
Command Line Arguments
Random Numbers
Re-Covering Pointers
Memory and Functions
Homework
2
Makefiles
list of rules you can call from the terminal
make ruleTwo will call the ruleTwo
make will call first rule in the file
basic formatting
use # at line beginning to denote comments
must use tab character, not 8 spaces
Rule Construction
target: dependencies (optional)
command
another command (optional)
target (rule name)
dependency (right side of colon)
command (explicit commands)
4
Creating a Rule
lets create a rule to compile and link the files
for Homework 4A:
hw4a.c
karaoke.c
karaoke.h
Creating a Rule
we need to, in order:
1. separately compile hw4a.c
2. separately compile karaoke.c
3. link hw4a.o and karaoke.o together
Creating a Rule
1. separately compile hw4a.c
well make a rule called hw4a.o
what command would we run in the terminal?
what files does it need to work?
Creating a Rule
1. separately compile hw4a.c
well make a rule called hw4a.o
what command would we run in the terminal?
what files does it need to work?
hw4a.c
so its dependent on hw4a.c
Creating a Rule
2. separately compile karaoke.c
well call this rule karaoke.o
what command will compile karaoke.c?
what files does it need to work?
Creating a Rule
2. separately compile karaoke.c
well call this rule karaoke.o
what command will compile karaoke.c?
what files does it need to work?
karaoke.c
so its dependent on karaoke.c
10
Creating a Rule
3. link hw4a.o and karaoke.o together
well call this rule hw4a
what command will link the files together?
what files does it depend on?
11
Creating a Rule
3. link hw4a.o and karaoke.o together
well call this rule hw4a
what command will link the files together?
what files does it depend on?
hw4a.o
karaoke.o
so its dependent on both of these files
12
13
Outline
Makefiles
File I/O
Command Line Arguments
Random Numbers
Re-Covering Pointers
Memory and Functions
Homework
16
scanf
stdin
input read in from user
redirection
executable < input.txt > output.txt
17
Opening a File
FILE* fopen (<filename>, <mode>);
<filename> is a string
<mode> is single-character string
19
20
23
Standard Streams in C
three standard streams: stdin, stdout, stderr
printf() and scanf() automatically access
stdout and stdin, respectively
printing to stderr prints to the terminal
even if we use redirection
25
fscanf
fscanf(ifp, %d, &inputInt);
LIVECODING
26
27
LIVECODING
28
29
Outline
Makefiles
File I/O
Command Line Arguments
Random Numbers
Re-Covering Pointers
Memory and Functions
Homework
30
32
34
in this example:
argc = ???
argv[0] is ???
argv[1] is ???
argv[2] is ???
35
in this example:
argc = 3 (executable, number, and city)
36
in this example:
argc = 3 (executable, number, and city)
argv[0] is ./hw2
37
in this example:
argc = 3 (executable, number, and city)
argv[0] is ./hw2
argv[1] is 25
38
in this example:
argc = 3 (executable, number, and city)
argv[0] is ./hw2
argv[1] is 25
argv[2] is Savannah
39
40
intArg = atoi(5);
intArg = atoi( argv[2] );
LIVECODING
41
Outline
Makefiles
File I/O
Command Line Arguments
Random Numbers
Re-Covering Pointers
Memory and Functions
Homework
44
Random Numbers
useful for many things:
cryptography, games of chance & probability,
procedural generation, statistical sampling
45
Pseudo Randomness
Anyone who considers arithmetical methods
of producing random digits is, of course, in a
state of sin. John von Neumann
pseudorandom
appears to be random, but actually isnt
mathematically generated, so it cant be
46
51
Outline
Makefiles
File I/O
Command Line Arguments
Random Numbers
Re-Covering Pointers
Memory and Functions
Homework
53
55
0xFFC0
56
0xFFC0
xs value is 5
5
value
57
xs value is 5
xs address is 0xFFC0
0xFFC0
address
value
58
0xFFC0
address
value
59
0xFFC0
address
value
60
address
value
61
ptr = &x
0xFFC0
address
value
62
0xFFC0
address
value
63
0xFFC0
address
value
64
0xFFC0
address
value
65
YES!!!
0xFFC0
address
value
66
0xFFC0
address
value
67
0xFFC0
0xFFC0
value
address
value
68
0xFFC0
0xFFC0
value
address
value
69
0xFFC0
0xFFC0
value
address
value
70
0xFFC4
0xFFC0
0xFFC0
address
value
address
value
71
0xFFC4
0xFFC0
0xFFC0
address
value
address
value
72
0xFFC4
0xFFC0
0xFFC0
address
where its
stored in
memory
value
address
value
73
0xFFC4
0xFFC0
0xFFC0
address
where its
stored in
memory
value
where it
points to in
memory
address
value
74
Stack
4 GB
address
space
0xFFFFFFF
Heap
Function calls,
locals
Dynamically
allocated
memory
Global/static vars
data segment
Code
code segment
75
the process
a chunk of memory given by the OS about 4 GB
the program
memory (on the stack) given to it by the process
you
when you dynamically allocate memory in the program
(memory given to you by the process )
76
the OS
the OS
the OS
the OS
77
the OS
the OS
the OS
Stack
Heap
Global/static vars
Code
78
Stack
Heap
Global/static vars
Code
79
Code
80
Code
81
82
Heap
83
Heap
84
Stack
Heap
85
Heap
86
Stack
Heap
87
CAR* train;
char* userStr;
int* intArray;
Stack
Heap
88
CAR* train;
char* userStr;
int* intArray;
Stack
?
train
Heap
userStr
intArray
89
Stack
?
train
Heap
userStr
intArray
90
Stack
?
train
Heap
userStr
intArray
91
Stack
Heap
train
userStr
intArray
92
Heap
train
userStr
intArray
93
Stack
(also program
variables)
Heap
train
userStr
intArray
94
Stack
(also program
variables)
Heap
train
userStr
intArray
95
return fxnAnswer;
Stack
(also program
variables)
Heap
train
userStr
intArray
96
return fxnAnswer;
Stack
(also program
variables)
Heap
train
userStr
intArray
97
Stack
(also program
variables)
Heap
train
userStr
intArray
98
Stack
(also program
variables)
Heap
train
userStr
intArray
99
Stack
free(intArray);
(also program
variables)
Heap
train
userStr
intArray
100
Stack
free(intArray);
(also program
variables)
Heap
train
userStr
intArray
101
Stack
(also program
variables)
Heap
train
userStr
intArray
102
Stack
(also program
variables)
Heap
train
userStr
intArray
103
(also program
variables)
Heap
train
userStr
intArray
104
Heap
train
userStr
intArray
105
Stack
(also program
variables)
Heap
train
userStr
intArray
106
Stack
(also program
variables)
Heap
train
userStr
intArray
107
Stack
(also program
variables)
Heap
train
userStr
intArray
108
Stack
(also program
variables)
Heap
train
userStr
intArray
109
(also program
variables)
Heap
train
userStr
intArray
110
Stack
(also program
variables)
Heap
train
userStr
intArray
111
Stack
(also program
variables)
Heap
train
userStr
intArray
112
Heap
train
userStr
intArray
113
Heap
train
userStr
intArray
114
Stack
(also program
variables)
NULL
Heap
train
userStr
intArray
115
NULL
Heap
train
userStr
intArray
116
Stack
(also program
variables)
NULL
Heap
train
userStr
intArray
117
program variables
Stack
(also program
variables)
NULL
Heap
train
userStr
intArray
118
program variables
Stack
intArray = (int*) malloc
(sizeof(int) * HUGE_NUM)
(also program
variables)
NULL
Heap
train
userStr
intArray
119
program variables
Stack
intArray = (int*) malloc
(sizeof(int) * HUGE_NUM)
(also program
variables)
NULL
Heap
train
userStr
intArray
120
program variables
Stack
intArray = (int*) malloc
(sizeof(int) * HUGE_NUM)
(also program
variables)
NULL
Heap
train
userStr
intArray
121
(also program
variables)
NULL
Heap
train
userStr
intArray
122
NULL
Heap
train
userStr
intArray
123
Outline
Makefiles
File I/O
Command Line Arguments
Random Numbers
Re-Covering Pointers
Memory and Functions
Homework
125
passing by value
passing by reference
implicit: arrays, strings
explicit: pointers
126
pass ???
132
Outline
Makefiles
File I/O
Command Line Arguments
Random Numbers
Re-Covering Pointers
Memory and Functions
Homework
138
Homework 4A
Karaoke
File I/O
command line arguments
allocating memory
no grade for Homework 4A
turn in working code or -10 points for HW 4B
139
Quick Notes
answered questions from HW2 on Piazza
magic numbers
should use #defines as you code
not replace with #define after youre done
140