Title: CS 151
1CS 151
- Programming assignment 6 is due Friday before
midnight. - Be sure to check your score posting
2Pointer
- A variable, say int years, holds a value (stored
in memory). ... - A "pointer" holds the address of (points to)
another memory location. - In C, we use and when working with pointers.
3The "address of" operator,
- Given int years
- Then years is the address of this variable.
- Recall using the on variables in the scanf
function. scanf("d", years) - years is a pointer constant.
- is the "address of" operator.
- printf("d", years) will print something weird
like, 653104, the address of the years variable.
4The "de-reference" operator,
- An address of a variable can be called a
reference to the variable. - Like a card-catalog entry for a book -- It refers
to the book (but is not the book itself). - The unary operator is the inverse of .
- It gets the value that is referenced it
de-references the reference. - a is just the value of a
5Pointer variables
- A pointer variable holds an address of, or
reference to a variable. - An integer pointer variable can be declared as
- int p
- You can now use p to store addresses of integer
variables, e.g. - int a 5, p
- p a
- printf("d", p) / prints 5 /
6More Syntax
- int years, p, a
- years 20
- p years
- / p holds address of years /
- a p
- / p is value at address p, i.e. 20 /
- printf("d d d", years, p, p)
- printf("d", (int )(6553104))
- Output 20 6553104 20 20
7What does it look like in memory?
20 years
6553104 6553108 6553112 6553116 6553120 65531
24 6553128 6553132 6553136 6553140
name value years 20 years 6553104 p
6553104 p 6553124 p 20
int years, p years 20 p years
6553104 p
8Change the value in years using the pointer
10 years
6553104 6553108 6553112 6553116 6553120 65531
24 6553128 6553132 6553136 6553140
int years, p years 20 p years p 10
6553104 p
9Not recommended for beginners!
11 years
6553104 6553108 6553112 6553116 6553120 65531
24 6553128 6553132 6553136 6553140
int years, p years 20 p years p
10 p
6553104 p
because it's equivalent to (p)
10Tricky!!!
15 years
6553104 6553108 6553112 6553116 6553120 65531
24 6553128 6553132 6553136 6553140
int years, p years 20 p years p
10 p p 15
6553108 p
because it's equivalent to (p)
11Call by Reference (use pointers)
- include ltstdio.hgt
- void swapints(int p, int q) / note
declaration / - int main( void)
- int a2, b5
- printf("d d\n", a, b) / prints 2 5 /
- swapints(a, b) / note actual parameters /
- printf("d d\n", a, b) / now prints 5 2
/ - return 0
-
- void swapints( int p, int q)
- int tmp
- tmp p / save a copy of what was in p /
- p q / now p holds what q holds /
- q tmp / swap is complete /
12On Entering swapints
a
2
1000 1004 1008 1016 1020 1024 1028 1032 10
36 1040 1044 1048
void swapints( int p, int q) int tmp tmp
p p q q tmp
b
5
p
1000
q
1004
tmp
13First Statement
a
2
1000 1004 1008 1016 1020 1024 1028 1032 10
36 1040 1044 1048
void swapints( int p, int q) int tmp tmp
p p q q tmp
b
5
p
1000
q
1004
tmp
2
14Second Statement
a
5
1000 1004 1008 1016 1020 1024 1028 1032 10
36 1040 1044 1048
void swapints( int p, int q) int tmp tmp
p p q q tmp
b
5
p
1000
q
1004
tmp
2
15Last Statement
a
5
1000 1004 1008 1016 1020 1024 1028 1032 10
36 1040 1044 1048
void swapints( int p, int q) int tmp tmp
p p q q tmp
b
2
p
1000
q
1004
tmp
2
16Pointers and Arrays
- The name of an array is a constant pointer to the
first element. - Given int odds5 3,5,7,9,11
- We can use the name odds as a constant pointer to
the beginning of the array. - Lets look at some of the possibilities ...
17Memory picture of an Array
- int odds5 3,5,7,9,11
- Thus odds has value 1000
odds
3 5 7 9 11
1000 1004 1008 1016 1020 1024 1028 1032 10
36 1040 1044 1048
18Using Array Names
- int odds5 3,5,7,9,11, p, n
- p odds
- / p now points to the first element of
odds, which contains 3 / - n odds
- / n now contains 3 /
- p odds 1
- / p now points to the second element of odds,
which contains 5 / - n p
- / n now contains 5 /
- n (odds 2)
- / n now contains 7 /
19Pointer Heaven (Hell)
- int odds5 3,5,7,9,11, p, n
- p odds / p points to 3 /
- p odds0 / same thing /
- n p / n is now 3 /
- n p0 / same thing /
- p4 13 / odds4 is now 13 /
20Pointer Arithmetic
- int odds5 3,5,7,9,11, p, n
- p odds / p points to 3 /
- p / p points to 5 /
- p (odds 1) / p points to 5 /
- p (odds 4) / p points to 11 /
- p odds4 / same thing, points to 11 /
21Array Parameters for Functions
- In prototype or definition, type required.
- In call to function, only name of array.
- Given declaration int array5
- float ave(float array, int n) / prototype/def
/ - float ave(float array, int n) / same thing /
- x ave(array, n) / call / ...
22Differences between Pointer Variables and Array
Names
- Array names are CONSTANT pointers, i.e. they are
addresses of values in memory. - odds or odds p are ILLEGAL!
- Pointers variables are VARIABLES, i.e. they are
values in memory - p p 5 p-- are all legal and useful,
but note that ordinary arithmetic is out the
window. - The result of adding to (or subtracting from) a
pointer depends entirely on the size of what it
points to.
23The sizeof function
- Returns the number of bytes used by a variable or
a data type. - Examples
- int list200
- double temp 28.7
- int n sizeof(int) / n contains 4 /
- n sizeof(temp) / n contains 8 /
- n sizeof(list) / n contains 800 /
- n sizeof(char) / n contains 1 /
24Dynamic Memory Allocation
- Getting Memory from the OS
- The Problem.
- We want to determine an array size when the
program runs, not when it is compiled - The Solution.
- Get memory for the array from the OS (Windows).
25The OS is "Keeper of the Heap"
- Each program in Win2000 or WinXP can have up to 4
GBytes of memory. - You just have to ask the Operating System to give
you some. - This process called memory allocation -- the OS
is allocating memory for your use. - Memory from OS is from the "heap"
26void malloc(int size)
- Stands for "memory allocation".
- size is number of bytes you want.
- returns a pointer to the first byte of your newly
allocated memory. - cast it into type of pointer you want ( char ,
int , etc.) - Use the memory however you like.
27Example Usage
- int p
- p (int ) malloc (1000 sizeof(int))
- / p can be considered an array of 1000 ints /
- for (i0 i lt 1000 i)
- pi rand() / init to random numbers /
28void free(void ptr)
- Gives the memory back to OS.
- ptr is pointer to beginning of allocated memory.
- Programs that don't give back are said to have
"memory leaks". - Unused memory that is not freed is unusable and
called garbage. (C has no garbage collection)
29calloc and realloc
- Two other members of the alloc family.
- calloc gets cleared memory, i.e. memory with all
bytes initialized to zero. - realloc will resize your allocated memory to a
larger or smaller size.
30Questions?
- Quiz 5 will be posted this evening through
Friday midnight. - Programming assignment 6 is due Friday before
midnight. - Be sure to check your score posting