Transcript Pointers

Pointers
(Continuation)
1
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
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
Pointer to void
4
Pointer to void
5
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
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
Pointer to Node
8
9
(Continued)
10
11
12
13
14
(Continued)
15
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
17
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
19
20
21
22
23
24
25
Algorithm Efficiency
Big-O Notation
26
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
f (n)  efficiency
where n is the number of elements to be
processed.
27
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
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
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)  n
30
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:
f (n)  log n
31
32
Linear Logarithmic Nested Loop
for (i=1; i<=10; i++)
for (j=1; j<=10; j *=2)
the loop body
• 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 the linear logarithmic nested loop the efficiency is determined
by the following formula:
f (n)  n log n
33
Quadratic Nested Loop
for (i=1; i<10; i++)
for (j=1; j<10; j ++)
the loop body
• 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 the quadratic nested loop the efficiency is determined
by the following formula:
f (n)  n
2
34
Dependent Quadratic Nested Loop
for (i=1; i<10; i++)
for (j=1; j<i; j ++)
the loop body
• 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 the dependent quadratic nested loop the efficiency is determined
by the following formula:
 n 1 
f ( n)  n 

2


35
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
Big-O notation
• The big-O notation can be derived from f(n)
using the following steps (pp. 32-33):
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,
 n 1  1 2 1
f (n)  n 
 n  n
2
 2  2
n2  n
O  f ( n)   O  n 2 
37
38
39
40
41
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
43
44
45
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
Homework
• Sections 1.5-1.8
• Exercises (Section 1.9): 2, 6, 7, 8, 20, 21
47