1 / 17

Pointers Contd.

Pointers Contd. 8. ptr. -5. ptr2. 8. ptr. -5. ptr2. Causing a Memory Leak. int *ptr = new int; *ptr = 8; int *ptr2 = new int; *ptr2 = -5;. ptr = ptr2;. 8. ptr. 8. ptr. NULL. ptr2. -5. ptr2. Leaving a Dangling Pointer. int *ptr = new int; *ptr = 8; int *ptr2 = new int;

Télécharger la présentation

Pointers Contd.

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.


Presentation Transcript

  1. Pointers Contd.

  2. 8 ptr -5 ptr2 8 ptr -5 ptr2 Causing a Memory Leak int *ptr = new int; *ptr = 8; int *ptr2 = new int; *ptr2 = -5; ptr = ptr2;

  3. 8 ptr 8 ptr NULL ptr2 -5 ptr2 Leaving a Dangling Pointer int *ptr = new int; *ptr = 8; int *ptr2 = new int; *ptr2 = -5; ptr = ptr2; delete ptr2; ptr2 = NULL;

  4. array array array array idx=3 3.4 2.6 5.7 Dynamic Array Example Sort an array of float numbers from standard input. The first element is the size of the array. For example: 5 3.4 2.6 5.7 100.4 43.5 sortFromInput( ) { float *array; int size, idx; cin >> size; array = new float[size]; for (idx=0; idx<size; idx++) cin >> array[idx]; Sort(array, size); OutputSortedArray(array, size); delete[] array; }

  5. Pointer Arithmetic • Int *p; • p = new int[10] • p[i]; 0 <= i <= 9 • What are *p, *p++ etc..

  6. Algorithm Efficiency and Sorting Algorithms

  7. Measuring the Efficiency of Algorithms • Analysis of algorithms is the area of computer science that provides tools for contrasting the efficiency of different methods of solution. • Concerns with significant differences

  8. How To Do Comparison? • Implement the algorithms in C++ and run the programs • How are the algorithms coded? • What computer should you use? • What data should the programs use? • Analyze algorithms independent of implementations

  9. The Execution Time of Algorithms • Count the number of basic operations of an algorithm • Read (get), write (put), compare, assignment, jump, arithmetic operations (increment, decrement, add, subtract, multiply, divide), shift, open, close, logical operations (not/complement, AND, OR, XOR), …

  10. The Execution Time of Algorithms • Counting an algorithm’s operations int sum = item[0]; int j = 1; while (j < n) { sum += item[j]; ++j; } <- 1 assignment <- 1 assignment <- n comparisons <- n-1 plus/assignments <- n-1 plus/assignments Total: 3n operations

  11. Algorithm Growth Rates • Measure an algorithm’s time requirement as a function of the problem size • Number of elements in an array Algorithm A requires n2/5 time units Algorithm B requires 5n time units Algorithm efficiency is a concern for large problems only

  12. Common Growth-Rate Functions - I

  13. Common Growth-Rate Functions - II

  14. Big O Notation Algorithm A is order f(n)-denoted O(f(n))-if constants k and n0 exist such that A requires <= k*f(n) time units to solve a problem of size n>=n0 • n2/5 • O(n2): k=1/5, n0=0 • 5*n • O(n): k=5, n0=0

  15. More Examples • How about n2-3n+10? • O(n2) if there exist k and n0 such that • kn2≥ n2-3n+10 for all n ≥ n0 • 3n2 ≥ n2-3n+10 for all n ≥ 2; so k=3, n0=2

  16. Properties of big-Oh • Ignore low-order terms • O(n3+4n2+3n)=O(n3) • Ignore multiplicative constant • O(5n3)=O(n3) • Combine growth-rate functions • O(f(n)) + O(g(n)) = O(f(n)+g(n))

  17. Worst-case vs. Average-case Analyses • An algorithm can require different times to solve different problems of the same size. • Worst-case analysis (find the maximum number of operations an algorithm can execute in all situations) • is easier to calculate and is more common • Average-case (enumerate all possible situations, find the time of each of the m possible cases, total and dive by m) • is harder to compute but yields a more realistic expected behavior

More Related