1 / 43

ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers

ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays – images and image representations. Array Example 1

ethan
Download Presentation

ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of 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. ES 314 Advanced Programming Lec 3 • Sept 8 • Goals: • complete discussion of pointers • discuss 1-d array examples • Selection sorting • Insertion sorting • 2-d arrays – images and image representations

  2. Array Example 1 Write a program that finds the largest number in a given collection of keys. Assume the numbers are stored in an array. int max (int[] A, int size)

  3. Array Example Write a program that finds the largest number in a given collection of keys. Assume the numbers are stored in an array. int max (int[] A, int size) { if (size == 0) return 0; int temp = A[0]; for (int j = 1; j < size; ++j) if (temp < A[j]) temp = A[j]; return temp; }

  4. Implementation using a vector int max (vector<int> A, int n) { if (n == 0) return 0; int temp = A[0]; for (int j = 1; j < size; ++j) if (temp < A[j]) temp = A[j]; return temp; }

  5. Notion of time complexity int max (int[] A, int n) { if (n == 0) return 0; int temp = A[0]; for (int j = 1; j < size; ++j) if (temp < A[j]) temp = A[j]; return temp; } What is the total number of operations performed by the above procedure (as a function of n)? Assignment, comparison, return – each costs one unit.

  6. Pointers • A pointer is a variable used to store an address • The declaration of a pointer must have a data type for the address the pointer will hold (looks like this): int *ptr; char *chptr;

  7. Location Behavior • A variable is a location • When locations are used in code, they behave differently depending on whether or not they are used on the left side of an assignment • If not used on the left side of an assignment, the value at the location is used • When used on the left side of assignment, the location itself is used

  8. Address-of operator • An address can be assigned to a pointer using the address-of operator &: int *ptr; int x; ptr = &x;

  9. Pointer variable, its value ptr x Addresses 00110 01010 01110 10010 10110 x ptr 01010

  10. Dereference Operator • The dereference operator, *, is used on a pointer (or any expression that yields an address) • The result of the dereference operation is a location (the location at the address that the pointer stores) • Therefore, the way the dereference operator behaves depends on where it appears in code (like variables)

  11. A simple exampleint x = 3, *ptr; ptr 3 x

  12. Assignment ptr = &x; ptr 3 x

  13. Result of assignment ptr = &x; ptr 3 x

  14. Another assignment y = *ptr + 5; ptr 3 x y

  15. Another assignment y = *ptr + 5; ptr 3 x 8 y

  16. More assignmentint z = 5; ptr 3 x 8 y 5 z

  17. effect of assignment *ptr = 10 + z; ptr 3 x 8 y 5 z

  18. *ptr = 10 + z; ptr 15 x 8 y 5 z

  19. *ptr = 10 + z; ptr 15 x Note that the value of x changes even though this line of code doesn’t contain x 8 y 5 z

  20. Exercise: What is the output produced by the following program? #include <iostream> int main() { int * p; int * q; int x; p = &x; *p = 6; cout << x << endl; q = p; *q = 8; cout << *p << endl; q = new int; *q = 9; cout << *q << endl; return 0; }

  21. Arrays • The address of an array is the same as the address of the first element of the array. • An array’s name (without using an index) contains the address of the array. • The address of the array can be assigned to a pointer by assigning the array’s name to the pointer. • An array name is not a pointer because the address in it cannot be changed (the address in a pointer can be changed).

  22. The [ ] Operator • When an array is indexed, [ ] is an operator. • For example, nums[3] produces the result *(nums + 3). • C++ produces an address from the expression nums + 3, by: • Noting what data type is stored at the address that nums contains • Multiplying by 3 the number of bytes in that data type • Adding the product onto the address stored in nums • After the address is produced from nums + 3, it is dereferenced to get a location.

  23. The Heap • The heap is a special part of RAM memory reserved for program usage. • When a program uses memory from the heap, the used heap memory is called dynamically-allocated memory. • The only way to dynamically allocate memory (use memory in the heap) is by using the new operator.

  24. The new Operator • The new operator has only one operand, which is a data type. • The new operation produces the address of an unused chunk of heap memory large enough to hold the data type (dynamically allocated) • In order to be useful, the address must be assigned to a pointer, so that the dynamically allocated memory can be accessed through the pointer: int *ptr; ptr = new int; *ptr = 5;

  25. int *ptr; ptr

  26. ptr = new int; ptr

  27. ptr = new int; ptr

  28. ptr = new int; Found a block in heap 110110 ptr

  29. ptr = 110110; 110110 ptr

  30. ptr = 110110; 110110 ptr

  31. *ptr = 5; (what happens?) 110110 ptr

  32. *ptr = 5; 110110 ptr 5

  33. Dynamically-allocated memory • Has no name associated with it (other locations have variable names associated with them) • Can only be accessed by using a pointer • The compiler does not need to know the size (in bytes) of the allocation at compile time • The compiler doesneed to know the size (in bytes) of any declared variables or arrays at compile time

  34. Exercise: What is the output produced by the following program? #include <iostream> int main() { int * p; int * q; int x; p = &x; *p = 6; cout << x << endl; q = p; *q = 8; cout << *p << endl; q = new int; *q = 9; cout << *q << endl; return 0; }

  35. Memory Leak First, pointer ptr is assigned the address of dynamically allocated memory. 110110 ptr 5

  36. Pointer assigned again ptr = new int; (what happens?) 110110 ptr 5

  37. ptr = new int; 111000 Unused block found in heap 110110 ptr 5

  38. Pointer assignment changes 111000 110110 ptr 5

  39. Pointer assignment 111000 110110 ptr 5

  40. Memory location inaccessible 111000 110110 ptr 5 The address of this block is not stored anywhere, but it hasn’t been freed – memory leak

  41. Avoiding Memory Leak • When you want to change the address stored in a pointer, always think about whether or not the current address is used for dynamically-allocated memory • If it is (and that memory is no longer useful), use the delete operator before changing the address in the pointer; for example, delete ptr;

  42. Pointers to Objects • Pointers can be used to point to objects or arrays of objects: Check *chkptr = new Check; Check *chkarrptr = new Check [10]; • The delete operator is used the same way: delete chkptr; delete [ ] chkarrptr;

  43. Running out of Heap Memory • We can run out of heap memory if: • We write poor code that continually allows memory leak while constantly using the new operator • OR … • If we use excessive amounts of heap memory • If the new operator cannot find a chunk of unused heap memory large enough for the data type, the new operation throws an exception

More Related