1 / 47

Pointers

Pointers. (Continuation). 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.

Download Presentation

Pointers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Pointers (Continuation)

  2. 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.

  3. 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.

  4. Pointer to void

  5. Pointer to void

  6. Pointer to void • Important remark: a pointer to void cannot be dereferencedunless it is cast. • In other words, we cannot use *p without casting (without connection of the pointer with particular data type).

  7. 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))

  8. Pointer to Node

  9. (Continued)

  10. (Continued)

  11. 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.

  12. 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.

  13. Algorithm Efficiency Big-O Notation

  14. 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.

  15. 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.

  16. 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.

  17. 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:

  18. Logarithmic Loops • In a logarithmic loop, the controlling variable is multiplied or divided in each iteration • For example: Multiply loopDivide 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:

  19. 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:

  20. 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:

  21. 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:

  22. 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) .

  23. Big-O notation • The big-O notation can be derived from f(n) using the following steps (pp. 32-33): • In each term set the coefficient of the term to 1. • 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,

  24. 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)

  25. 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)

  26. Homework • Sections 1.5-1.8 • Exercises (Section 1.9): 2, 6, 7, 8, 20, 21

More Related