Revision on Pointers, Structures - PowerPoint PPT Presentation

1 / 80
About This Presentation
Title:

Revision on Pointers, Structures

Description:

First, determine the base case. Determine the general case. ... Problem: Size determined at compile time. We must explicitly declare the exact size. ... – PowerPoint PPT presentation

Number of Views:32
Avg rating:3.0/5.0
Slides: 81
Provided by: web57
Category:

less

Transcript and Presenter's Notes

Title: Revision on Pointers, Structures


1
Revision on Pointers, Structures Recursion
2
Revision on Pointers
3
Figure 9-1
4
Figure 9-2
5
Figure 9-3
6
Figure 9-4
7
Figure 9-5
8
Figure 9-6
9
Figure 9-7
10
Figure 9-8
11
Figure 9-9
12
Figure 9-10
13
Figure 9-11
14
Figure 9-12
15
Figure 9-13
16
Figure 9-14
17
Figure 9-15
18
Figure 9-16
19
Figure 9-7
20
Figure 9-18
21
Figure 9-19
22
Figure 9-20
23
Figure 9-21
24
Revision on Structures
25
Figure 12-7
26
Figure 12-8
27
Figure 12-9
28
Figure 12-10
29
Figure 12-11
30
Figure 12-12
31
Figure 12-13
32
Figure 12-14
33
Figure 12-15
34
Figure 12-16
35
Figure 12-17
36
Figure 12-18
37
Figure 12-19
38
Figure 12-20
39
Figure 12-21
40
Figure 12-22
41
Figure 12-23
42
Figure 12-24
43
Figure 12-25
44
Revision of Recursion
45
The 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

46
Examples of Recursion
47
Figure 6-1
48
Figure 6-2
49
Figure 6-3
50
Figure 6-4
51
Figure 6-5
52
Figure 6-6
53
Figure 6-9
54
Designing 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.

55
When 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)

56
What is the answer?
  • Yes, then we use recursion.
  • No, maybe use iterative solution.

57
A classic Solution to the Tower of Hanoi
Optional
58
The 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/

59
Figure 6-10
Optional
60
Figure 6-11
Optional
61
Solution 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.

62
Figure 6-12, Part I
Optional
63
Figure 6-12 Part II
Optional
64
Figure 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
65
Moving 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

66
Using Dynamic Memory
67
Pointers 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.

68
Pointer 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.

69
void
  • 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.

70
When 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?

71
Stack 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

72
Two 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

73
A 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

74
but 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

75
Dont 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

76
Dynamic 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.

77
Dynamic 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)

78
Using 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'

79
mallocing 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)

80
Further 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).
Write a Comment
User Comments (0)
About PowerShow.com