Title: I hope you:
1CS110 Lecture 18Pointers, Functions Memory
Jack Tumblinjet_at_cs.northwestern.edu
- I hope you
- Have nearly finished PA-3 Due Monday 11/10
- Are nearly ready for Quiz 3 Wed 11/12 covers
Chapter 9. - Started reading Chap. 10.1-10.5
2(Recall) Example III
void my_swap3(int pa, int pb) int main
(void)int x10, y25 int px, py px
x py y swap3(px, py)
return 0 void swap3(int pa, int pb)int
tmp tmp pa pa pb pb
tmp
Computer Memory
address
var name value
x 10
900
y 25
904
908
px 900
COPY
COPY
912
py 904
pa 900
916
pb 904
920
924
3 NEW! Function Call Jargon
- Actual arguments The values used during
function calling (a.k.a. actual parameter) - Formal parameters The functions local
variables in the argument list (these vars. are
declared in the function prototype).
formal parameters
actual arguments
int add (int x, int y) return (xy)
int main(void) add(3,4) return 0
4 Function Call Jargon
- At the start of a function call,
- The actual arguments are copied to the formal
parameters (an assignment) - IMPORTANT! Know these two terms well!
formal parameters
actual arguments
int main(void) add(3,4) return 0
int add (int x, int y) return (xy)
5(Recall) Function and Arrays
- The actual argument below is an array name it
holds address of 0th byte in array, (a constant) - The formal parameter is a local array name
copies the address from the argument
actual argument
formal parameter
int main(void) double arr20.0,0.0
dset(arr,1) return 0
void dset(double x, int y) xy 42.0
6Pass by Reference
- Parameter y copies a VALUE to use in function
- Important Jargon pass by value
- Parameter x copies the ADDRESS of value(s) to
use it refers to the values without copying - Important Jargon pass by reference
actual arguments
formal parameters
int main(void) double arr20.0,0.0
dset(arr,1) return 0
void dset(double x, int y) xy 42.0
7Pass by Reference
- Array arguments are passed by reference to
functionsand can be BOTH input output. - (both main() and dset() can set array element
values). - Pointers let us pass ANY data by reference
actual arguments
formal parameters
int main(void) double arr20.0,0.0
dset(arr,1) return 0
void dset(double x, int y) xy 42.0
8Pass by Reference
- Function arguments that are pointers
- Copy an address to the formal parameters
- Indexing formal parameter lets us modify the
value at or near the pointer ? pass by
reference
actual arguments
formal parameters
int main(void) double val1.234double pd
pd val dfixit(val) return 0
void dfixit(double x) x0 42.0
9Pass by Reference
- Compact, fast, efficient (copies just the
address, not all the data) - Powerful arguments can be input, output, or
both - ?!DANGER!?
- Gave a function some invalid pointer
arguments?Expect the worst kinds of havocthe
wrong thing stored in the wrong place at the
wrong time!!
10Pointer Return Types
- Functions whose return type is a pointer?
- HOW function prototype looks like this
- char findNextVowel( char str)
- ? ! MORE POINTER DANGER ! ?
- All of a functions variables are local and
temporary they may vanish when you leave the
function - Dont return pointers to local variables!
- Better idea return pointers received as inputs
(a very common practice. See examples in
book) - Try again Never return pointers to local
variables ?UNLESS? you used the static
keyword
11Function Pointer Caution I
int main (void) int pscore int num2
num0 32 num1 16 pscore num
Jprnt(pscore) pscore 5 return 0
(copy value of pscore (an address) into ptr
void Jprnt(int ptr) printf(d, ptr)
ptr ptr1
BAD! When print exits, ptr dies!
pscore value was not changed at all by the
Jprnt() function!
12Function Pointer Caution II
int main(void) int pscore, num num
32 pscore incr(num) pscore num
return 0
int incr(int x) x x10 return x
!!NOOOO!!! When incr() exits, variable x dies!
Out of scope its memory may get reassigned to a
different variable, maybe now, later, or never.
Main function LOOKS ok, but isnt pscore is now
an invalid pointer, but youll get no error
message and no warning. DEBUG NIGHTMARE!
13static keyword
- static int i / put before data
type /static double arr5 / OK for
arrays ptrs / static void setKey(int a) /
and functions. / - WEIRD! (at least) 2 meanings
- 1) only accessible from inside this module, or
- (module is one .c file and its .h file
interface) - Sets scope for functions cant call them from
interface - And for global variables, global arrays (See
pg. 359, 410) - 2) Always keep this named memory valid
- Use for local variables, declared within a
function - DIFFERS from global variable name can STILL go
out-of-scope - But pointers to it (set when name was in-scope)
always work! -
14static Example
- int magic_num(int strt) / prototype/
- int main(void)int i0, pI / ptr to
integer / - pI magic_num(i) printf(random
4fibd\n,pI) return 0int
magic_num(int strt)static int keep9
0,1,2,3,5,8,13,21,34int pOut - pOut (keepstrtrand4)9
- return(pOut)
Result gtrandom 4fib3 gt
keep array stays valid in memory, but strt and
pOut do not.
15(Recall) Arrays vs. Pointers
- An array resides in a named block of memory.
- Array size is constant fixed when program is
written.int list4 - Pointers can point to blocks of memory
(like a movable array name) but have no elements
of its own.how can they get their own memory
blocks to control? - What if you want to vary array size, or want to
choose array size after the program starts
running? (at run time?)
d n 0 0 0 3 z p 0 0 0 1 0 0 0 6 0 0 0 4 0 0 0 8
j
list0
list1
list2
list3
p1
16Dynamic Allocation
- Dynamic Allocation memory taken and/or
released during execution. - Dynamic Allocation enables us to
- claim a computed of elements at run time
(function malloc) - release all elements you reserved
(function free) - Less important variants (OK to ignore them) can
- change the number of elements (function realloc)
- claim already-zero-valued memory (function
calloc) - Not worth usingslow, inflexible. Write your
own...