Title: Revision on Pointers, Structures
1Revision on Pointers, Structures Recursion
2Revision on Pointers
3Figure 9-1
4Figure 9-2
5Figure 9-3
6Figure 9-4
7Figure 9-5
8Figure 9-6
9Figure 9-7
10Figure 9-8
11Figure 9-9
12Figure 9-10
13Figure 9-11
14Figure 9-12
15Figure 9-13
16Figure 9-14
17Figure 9-15
18Figure 9-16
19Figure 9-7
20Figure 9-18
21Figure 9-19
22Figure 9-20
23Figure 9-21
24Revision on Structures
25Figure 12-7
26Figure 12-8
27Figure 12-9
28Figure 12-10
29Figure 12-11
30Figure 12-12
31Figure 12-13
32Figure 12-14
33Figure 12-15
34Figure 12-16
35Figure 12-17
36Figure 12-18
37Figure 12-19
38Figure 12-20
39Figure 12-21
40Figure 12-22
41Figure 12-23
42Figure 12-24
43Figure 12-25
44Revision of Recursion
45The meaning of recursion
- Recursion is a repetitive process in which an
algorithm calls itself. - We often draw the part of the tree showing the
recursive calls and we call it a recursion tree. - The normal implementation of recursion are kept
on stack. The number of space needed for this
stack is proportional to the height of the
recursion tree. - The amount of space needed for this stack is
proportional to the height of the recursion tree
NOT the total number of node in the tree. - The amount of space needed depends on the depth
of recursion, not on the number of time the
function is invoke
46Examples of Recursion
47Figure 6-1
48Figure 6-2
49Figure 6-3
50Figure 6-4
51Figure 6-5
52Figure 6-6
53Figure 6-9
54Designing Recursion
- Objective Every recursive call must either solve
the problem or reduce the size of the problem. - Method(Divide Conquer Combine)
- First, determine the base case.
- Determine the general case.
- Combine the base case and the gereral case in to
an algorithm.
55When should we use recursion?
- You should NOT use recursion if the answer to the
following question is no! - Is the algorithm or data structure naturally
suitable for recursion? - Is the recursive solution shorter and more easily
to understand? - Does the recursive solution runs within
reasonable space (memory usage) and time (CPU
cycles)
56What is the answer?
- Yes, then we use recursion.
- No, maybe use iterative solution.
57A classic Solution to the Tower of Hanoi
Optional
58The rules
Optional
- Only one disk could be moved at a time. A larger
disk must never be stacked above a smaller one. - One and only one auxiliary needle could be used
for the intermediate storage of disks. - Interactive demo
- http//www.mazeworks.com/hanoi/
59Figure 6-10
Optional
60Figure 6-11
Optional
61Solution for Three Disks
Optional
- Move n-1 disks from source to auxiliary
- Move one disk from source to destination.
- Move n-1 disks from auxiliary to destination.
62Figure 6-12, Part I
Optional
63Figure 6-12 Part II
Optional
64Figure 6-13
Calls Output Towers (3, A, C, B) Towers
(2, A, B, C) Towers (1, A, C, B) Step 1 Move
from A to C Step 2 Move from A to B Towers
(1, C, B, A) Step 3 Move from C to
B Step 4 Move from A to C Towers (2, B, C,
A) Towers (1, B, A, C) Step 5 Move from B to
A Step 6 Move from B to C Towers (1, A, C,
B) Step 7 Move from A to C
Optional
65Moving 64 Disks?
Optional
- Consider the recursion tree, we can calculate the
instruction needed to 64 disks. - One instruction is printed for each vertex in the
tree, except for the leaves (which calls with
n0). The number of node-leaves - 12 4 263
- 20 21 22 263 264 - 1
66Using Dynamic Memory
67Pointers and Arrays
- int main()
- int array10
- int pntr array
- for(int i0 i lt 10 i)
- printf(d\n, pntri)
-
- return 0
-
- We can get a pointer to the beginning of an array
using the name of the array variable without any
brackets. - From then on, we can index into the array using
our pointer.
68Pointer Arithmetic
- int main()
- int array10
- int pntr NULL //set pointer to NULL
- for(pntr array pntr lt array 10 pntr)
- printf(d\n, pntr) //dereference the pntr
-
- return 0
-
- We can increment a pointer, which has the
effect of making it point to the next variable in
a array. - Instead of having an integer counter, we iterate
through the array by moving the pointer itself. - The pointer is initialized in the for loop to the
start of the array. Terminate when we get the
tenth index.
69void
- A pointer to nothing??
- NO!! A pointer to anything
- int main()
- char c
- int i
- float f
- void ptnr
- ptnr c //OK
- ptnr i //OK
- ptnr f //OK
- return 0
-
- Remember, all pointers are the same size
(typically 32 or 64 bits) because they all store
the same kind of memory address.
70When to pass by pointer
- When declaring a function, you can either pass by
value or by reference. - Factors to consider
- How much data do you have?
- Do you trust the other functions that will be
calling your function. - Can you handle the memory management complexity?
71Stack vs. Heap
- Review from Computer Architecture Stack vs. Heap
- Both are sources from which memory is allocated
- Stack is automatic
- Created when memory is in scope
- Destroyed when memory is out of scope
- Heap is manual
- Created upon request
- Destroyed upon request
- More detailed description of the difference
between heap and stack. see http//www.cs.jcu.edu
.au/Subjects/cp2003/1997/foils/heapAndStack/heapAn
dStack.html
72Two ways to get an int
- On the Stack
- int main()
- int myInt //declare an int on the stack
- myInt 5 //set the memory to five
- return 0
-
- On the Heap
- int main()
- int myInt malloc(sizeof(int))
- //allocate mem. from heap
- myInt 5 //set the memory to five
- return 0
-
73A closer look at malloc
- int main()
- int myInt malloc(sizeof(int))
- myInt 5
- return 0
-
- malloc is short form for memory allocation
- Takes as a parameter the number of bytes to take
from the heap - sizeof(int) conveniently tells us how many bytes
are in an int (usually 4) - Returns a pointer to the memory that was just
allocated
74but we forgot something!
- We requested memory but never released it!
- int main()
- int myInt malloc(sizeof(int))
- myInt 5
- free(myInt) //use free() to release memory
- myInt NULL
- return 0
-
- free() releases memory we arent using anymore
- Takes a pointer to the memory to be released
- Must have been allocated with malloc
- Should set pointer to NULL when done
75Dont do these things!
- free() memory on the stack
- int main()
- int myInt
- free(myInt) //very bad
- return 0
-
- Lose track of malloc()d memory
- int main()
- int myInt malloc(sizeof(int))
- myInt 0 //how can you free it now?
- return 0
-
- free() the same memory twice
- int main()
- int A malloc(sizeof(int))
- int B A
- free(A)
- free(B) //very bad
- return 0
76Dynamic arrays (1)
- Review static arrays
- int main()
- int array5 //static array
of size 5 - for(int i 0 i lt 5 i) //initialize
the array to 0 - arrayi 0
-
- return 0
-
- Problem Size determined at compile time. We must
explicitly declare the exact size.
77Dynamic arrays (2)
- Solution use malloc() to allocate enough space
for the array at run-time - include ltstdio.hgt
- include ltstdlib.hgt
- include lttime.hgt
- int main()
- srand( (unsigned)time( NULL ) )
- int n rand() 100 //random
number between 0 and 99 - int array (int) malloc(n sizeof(int))
//allocate array of size n using malloc -
- for(int i 0 i lt n i) //we can count
up to (n-1) in our array - arrayi i
-
-
- for(int i 0 i lt n i)
- printf(" d", arrayi)
-
78Using malloc for strings
- Since strings in C are just arrays of chars,
malloc can be used to create variable length
strings - include ltstdio.hgt
- include ltstdlib.hgt
- include lttime.hgt
- int main()
- srand( (unsigned)time( NULL ) )
-
- int n rand() 100 //random number
between 0 and 99 - char string malloc(n)
- int i
- //a char is one byte, so we dont have to use
sizeof - for(i 0 i lt n-1 i)
- stringi 'A'
-
79mallocing structs
- We can also use malloc to allocate space on the
heap for a struct - include ltstdlib.hgt
- include ltstdio.hgt
- typedef struct foo
- int value
- int array10
- foo_t
- int main()
- //sizeof(foo_t) gives us the size of the
struct - foo_t fooStruct (foo_t) malloc(sizeof(foo_t
)) - fooStruct-gtvalue 5
-
- for(int i 0 i lt 10 i)
- fooStruct-gtarrayi i
-
- printf("d\n", fooStruct-gtvalue)
80Further Reading
- The C Programming Language, Second Edition by
Brian W. Kernighan and Dennis M. Ritchie.
Prentice Hall, Inc., 1988. ISBN 0-13-110362-8
(paperback), 0-13-110370-9 (hardback).