pointers (continuation) 1. data pointer a pointer is a programming language data type whose value...

47
Pointers (Continuation) 1

Upload: valentine-parks

Post on 25-Dec-2015

223 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Pointers (Continuation) 1. Data Pointer A pointer is a programming language data type whose value refers directly to ("points to") another value stored

Pointers

(Continuation)

1

Page 2: Pointers (Continuation) 1. Data Pointer A pointer is a programming language data type whose value refers directly to ("points to") another value stored

Data Pointer

• A pointer is a programming language data type whose value refers directly to ("points to") another value stored elsewhere in the computer memory using its address. Obtaining the value that a pointer refers to is called dereferencing the pointer.

2

Page 3: Pointers (Continuation) 1. Data Pointer A pointer is a programming language data type whose value refers directly to ("points to") another value stored

Pointer to void

• Major programming languages are strongly typed. This means that operations such as assign and compare must use compatible types or be cast to compatible types.

• The only exception is the pointer to void, which can be assigned without a cast.

• This means that a pointer to void is a generic pointer that can be used to represent any data type.

3

Page 4: Pointers (Continuation) 1. Data Pointer A pointer is a programming language data type whose value refers directly to ("points to") another value stored

Pointer to void

4

Page 5: Pointers (Continuation) 1. Data Pointer A pointer is a programming language data type whose value refers directly to ("points to") another value stored

Pointer to void

5

Page 6: Pointers (Continuation) 1. Data Pointer A pointer is a programming language data type whose value refers directly to ("points to") another value stored

Pointer to void

• Important remark: a pointer to void cannot be dereferenced unless it is cast.

• In other words, we cannot use *p without casting (without connection of the pointer with particular data type).

6

Page 7: Pointers (Continuation) 1. Data Pointer A pointer is a programming language data type whose value refers directly to ("points to") another value stored

Function malloc• This function in C (a similar function is presented

in all modern programming languages) returns a pointer to void.

• This function is used to dynamically allocate any type of data.

• This is a generic function that returns a pointer to void (void*). It can be used for returning a pointer to any data type. For example, a pointer to an integer can be created using

intPtr = (int*)malloc (sizeof (int))7

Page 8: Pointers (Continuation) 1. Data Pointer A pointer is a programming language data type whose value refers directly to ("points to") another value stored

Pointer to Node

8

Page 9: Pointers (Continuation) 1. Data Pointer A pointer is a programming language data type whose value refers directly to ("points to") another value stored

9

Page 10: Pointers (Continuation) 1. Data Pointer A pointer is a programming language data type whose value refers directly to ("points to") another value stored

(Continued)

10

Page 11: Pointers (Continuation) 1. Data Pointer A pointer is a programming language data type whose value refers directly to ("points to") another value stored

11

Page 12: Pointers (Continuation) 1. Data Pointer A pointer is a programming language data type whose value refers directly to ("points to") another value stored

12

Page 13: Pointers (Continuation) 1. Data Pointer A pointer is a programming language data type whose value refers directly to ("points to") another value stored

13

Page 14: Pointers (Continuation) 1. Data Pointer A pointer is a programming language data type whose value refers directly to ("points to") another value stored

14

Page 15: Pointers (Continuation) 1. Data Pointer A pointer is a programming language data type whose value refers directly to ("points to") another value stored

(Continued)

15

Page 16: Pointers (Continuation) 1. Data Pointer A pointer is a programming language data type whose value refers directly to ("points to") another value stored

Pointer to Function

• Functions in our program occupy memory. The name of the function is a pointer constant to its first byte of memory.

• To declare a pointer to function, we code it as if it was a prototype definition, with the function pointer in parentheses.

16

Page 17: Pointers (Continuation) 1. Data Pointer A pointer is a programming language data type whose value refers directly to ("points to") another value stored

17

Page 18: Pointers (Continuation) 1. Data Pointer A pointer is a programming language data type whose value refers directly to ("points to") another value stored

Example: function larger

• This generic function will return a larger value of two values to be compared

• To use a larger function as a generic one, we will need to write a compare function for each particular data type. A compare function will return either a positive or negative flag value depending on which value in a compared pair is larger: the first one or the second one.

18

Page 19: Pointers (Continuation) 1. Data Pointer A pointer is a programming language data type whose value refers directly to ("points to") another value stored

19

Page 20: Pointers (Continuation) 1. Data Pointer A pointer is a programming language data type whose value refers directly to ("points to") another value stored

20

Page 21: Pointers (Continuation) 1. Data Pointer A pointer is a programming language data type whose value refers directly to ("points to") another value stored

21

Page 22: Pointers (Continuation) 1. Data Pointer A pointer is a programming language data type whose value refers directly to ("points to") another value stored

22

Page 23: Pointers (Continuation) 1. Data Pointer A pointer is a programming language data type whose value refers directly to ("points to") another value stored

23

Page 24: Pointers (Continuation) 1. Data Pointer A pointer is a programming language data type whose value refers directly to ("points to") another value stored

24

Page 25: Pointers (Continuation) 1. Data Pointer A pointer is a programming language data type whose value refers directly to ("points to") another value stored

25

Page 26: Pointers (Continuation) 1. Data Pointer A pointer is a programming language data type whose value refers directly to ("points to") another value stored

Algorithm Efficiency

Big-O Notation

26

Page 27: Pointers (Continuation) 1. Data Pointer A pointer is a programming language data type whose value refers directly to ("points to") another value stored

What is the algorithm’s efficiency

• The algorithm’s efficiency is a function of the number of elements to be processed. The general format is

where n is the number of elements to be processed.( ) efficiencyf n

27

Page 28: Pointers (Continuation) 1. Data Pointer A pointer is a programming language data type whose value refers directly to ("points to") another value stored

The basic concept

• When comparing two different algorithms that solve the same problem, we often find that one algorithm is an order of magnitude more efficient than the other.

• A typical example is a famous Fast Fourier Transform algorithm. It requires NxlogN multiplications and additions, while a direct Fourier Transform algorithm requires N2 multiplications and additions.

28

Page 29: Pointers (Continuation) 1. Data Pointer A pointer is a programming language data type whose value refers directly to ("points to") another value stored

The basic concept

• If the efficiency function is linear then this means that the algorithm is linear and it contains no loops or recursions. In this case, the algorithm’s efficiency depends only on the speed of the computer.

• If the algorithm contains loops or recursions (any recursion may always be converted to a loop), it is called nonlinear. In this case the efficiency function strongly and informally depends on the number of elements to be processed.

29

Page 30: Pointers (Continuation) 1. Data Pointer A pointer is a programming language data type whose value refers directly to ("points to") another value stored

Linear Loops

• The efficiency depends on how many times the body of the loop is repeated. In a linear loop, the loop update (the controlling variable) either adds or subtracts.

• For example:

for (i = 0; i < 1000; i++)

the loop body

Here the loop body is repeated 1000 times.

For the linear loop the efficiency is directly proportional to the number of iterations, it is:

( )f n n30

Page 31: Pointers (Continuation) 1. Data Pointer A pointer is a programming language data type whose value refers directly to ("points to") another value stored

Logarithmic Loops

• In a logarithmic loop, the controlling variable is multiplied or divided in each iteration

• For example:

Multiply loop Divide loop

for (i=1; i<=1000; i*=2) for (i=1000; i<=1; i /=2)

the loop body the loop body

For the logarithmic loop the efficiency is determined by the following formula:

( ) logf n n31

Page 32: Pointers (Continuation) 1. Data Pointer A pointer is a programming language data type whose value refers directly to ("points to") another value stored

32

Page 33: Pointers (Continuation) 1. Data Pointer A pointer is a programming language data type whose value refers directly to ("points to") another value stored

Linear Logarithmic Nested Loop

• The outer loop in this example adds, while the inner loop multiplies• A total number of iterations in the linear logarithmic nested loop is

equal to the product of the numbers of iterations for the external and inner loops, respectively (10log10 in our example).

for (i=1; i<=10; i++)

for (j=1; j<=10; j *=2)

the loop body

For the linear logarithmic nested loop the efficiency is determined

by the following formula:

( ) logf n n n33

Page 34: Pointers (Continuation) 1. Data Pointer A pointer is a programming language data type whose value refers directly to ("points to") another value stored

Quadratic Nested Loop

• Booth loops in this example add• A total number of iterations in the quadratic nested loop is equal to

the product of the numbers of iterations for the external and inner loops, respectively (10x10=100 in our example).

for (i=1; i<10; i++)

for (j=1; j<10; j ++)

the loop body

For the quadratic nested loop the efficiency is determined

by the following formula:

2( )f n n 34

Page 35: Pointers (Continuation) 1. Data Pointer A pointer is a programming language data type whose value refers directly to ("points to") another value stored

Dependent Quadratic Nested Loop

• The number of iterations of the inner loop depends on the outer loop. It is equal to the sum of the first n members of an arithmetic progression: (n+1)/2

• A total number of iterations in the quadratic nested loop is equal to the product of the numbers of iterations for the external and inner loops, respectively (10x5=50 in our example).

for (i=1; i<10; i++)

for (j=1; j<i; j ++)

the loop body

For the dependent quadratic nested loop the efficiency is determined

by the following formula:1

( )2

nf n n

35

Page 36: Pointers (Continuation) 1. Data Pointer A pointer is a programming language data type whose value refers directly to ("points to") another value stored

Big-O notation

• In general, the number of statements executed in the function for n elements of data is a function of the number of elements expressed as f(n).

• Although the equation derived for a function may be complex, a dominant factor in the equation usually determines the order of magnitude of the result.

• This factor is a big-O, as in “on the order of”. It is expressed as O(n) .

36

Page 37: Pointers (Continuation) 1. Data Pointer A pointer is a programming language data type whose value refers directly to ("points to") another value stored

Big-O notation

• The big-O notation can be derived from f(n) using the following steps (pp. 32-33):

21 1 1( )

2 2 2

nf n n n n

1. In each term set the coefficient of the term to 1.

2. Keep the largest term in the function and discard the others. Terms are ranked from lowest to highest: log n, n, n log n, n2, n3,…, nk,…2n,…, n!

For example,

2n n

2( )O f n O n 37

Page 38: Pointers (Continuation) 1. Data Pointer A pointer is a programming language data type whose value refers directly to ("points to") another value stored

38

Page 39: Pointers (Continuation) 1. Data Pointer A pointer is a programming language data type whose value refers directly to ("points to") another value stored

39

Page 40: Pointers (Continuation) 1. Data Pointer A pointer is a programming language data type whose value refers directly to ("points to") another value stored

40

Page 41: Pointers (Continuation) 1. Data Pointer A pointer is a programming language data type whose value refers directly to ("points to") another value stored

41

Page 42: Pointers (Continuation) 1. Data Pointer A pointer is a programming language data type whose value refers directly to ("points to") another value stored

Add two matrices

• In this algorithm, we see that for each element in a raw, we add all the elements in a column. This means that we have a quadratic loop here and the efficiency of the algorithm is O(n2)

42

Page 43: Pointers (Continuation) 1. Data Pointer A pointer is a programming language data type whose value refers directly to ("points to") another value stored

43

Page 44: Pointers (Continuation) 1. Data Pointer A pointer is a programming language data type whose value refers directly to ("points to") another value stored

44

Page 45: Pointers (Continuation) 1. Data Pointer A pointer is a programming language data type whose value refers directly to ("points to") another value stored

45

Page 46: Pointers (Continuation) 1. Data Pointer A pointer is a programming language data type whose value refers directly to ("points to") another value stored

Multiply two matrices

• In this algorithm, we see three nested loops. Because each loop starts at the first element, we have a cubic loop and the efficiency of the algorithm is O(n3)

46

Page 47: Pointers (Continuation) 1. Data Pointer A pointer is a programming language data type whose value refers directly to ("points to") another value stored

Homework

• Sections 1.5-1.8

• Exercises (Section 1.9): 2, 6, 7, 8, 20, 21

47