1 / 20

Main Index

Chapter 5 – Pointers and Dynamic Memory. 1. Main Index. Contents. Pointer Illustration Vertical / Horizontal View . . . Data Addresses in Memory Declaring Pointer Variables Assigning Values to Pointers Accessing Data with Pointers Arrays and Pointers Operator ‘new’ Operator ‘delete’

kylia
Download Presentation

Main Index

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. Chapter 5 – Pointers and Dynamic Memory 1 Main Index Contents • Pointer Illustration • Vertical / Horizontal View . . . • Data Addresses in Memory • Declaring Pointer Variables • Assigning Values to Pointers • Accessing Data with Pointers • Arrays and Pointers • Operator ‘new’ • Operator ‘delete’ • Illustrating the Destructor • Copy Constructor / Overloaded Assignment Operator • Declaration of dynamicClass Objects • The Pointer ‘this’ • dynamicClass Copy Constructor • The C++ Index Operator [] • Matrices • Summary Slides (3 pages)

  2. 2 Main Index Contents Pointer Illustration

  3. 3 Main Index Contents Vertical and Horizontal View of Memory

  4. 4 Main Index Contents Data Addresses in Memory

  5. type *ptr; int *intPtr; char *charPtr; 5 Main Index Contents Declaring Pointer Variables • Declare a pointer by stating the type followed by the variable name, but with a "*" added immediately before the name. • The pointer ptr is a variable whose value is the address of a data item of the designated type.

  6. Assigning Values to Pointers • &m is the address of the integer in memory. The assignment statement • sets intPtr to point at an actual data item. • The Figure illustrates the status of the two variables from the declaration statement and the resulting status after the assignment of a value to intPtr. int m = 50, *intPtr; intPtr = &m;

  7. 7 Main Index Contents Accessing Data with Pointers int x = 50, y = 100, *px = &x, *py = &y;

  8. 8 Main Index Contents Arrays and Pointers

  9. 9 Main Index Contents Operator ‘new’ p = new time24; // *p is 00:00 (midnight) q = new time24(8, 15); // *q is 8:15 AM

  10. Operator ‘delete’ • deallocating a dynamic array, use a slightly different form of delete. Place square brackets [] between delete and the pointer variable name. The system deallocates all of the memory originally assigned to the dynamic array. arr = new T[ARRSIZE]; // allocated space for ARRSIZE objects delete [] arr; // deallocate dynamic array storage

  11. 11 Main Index Contents Illustrating the Destructor

  12. Copy Constructor / Overloaded Assignment Operator

  13. 13 Main Index Contents Declaration of dynamicClass Objects

  14. IS the object *this objA; this-> memberl Points to the Object The Pointer ‘this’

  15. 15 Main Index Contents dynamicClass Copy Constructor Algorithm

  16. The C++ Index Operator [] arr[i] = 30; // arr[i] is the address into which 30 is copied t = arr[i] + 4; // add 4 to the value of the element at arr[i]

  17. 17 Main Index Contents Matrices • A Matrix is a two-dimensional array that corresponds to a row-column table of entries of a specified data type. • Matrices are referenced using a pair of indices that specify the row and column location in the table. Example: The element mat[0][3] is 2 The element mat[1][2] is 4.

  18. 18 Main Index Contents Summary Slide 1 §- Pointerscontain the address of data in memory… - Data is accessed by applying the dereference operator * §- Operators such as +, ++, and += apply to pointers. §- With such operators, pointers can be used for algorithms involving array traversal, but their primary application is in the allocation and maintenance of dynamic memory.

  19. 19 Main Index Contents Summary Slide 2 §- vector implementation - The miniVector class illustrates the key points. 1) It allocates dynamic memory using: destructor copy constructor overloaded assignment operator 2) It implements push_back(): Therefore it must control vector capacity in order to minimize dynamic memory reallocation. 3) It allows access to elements by using an index: Therefore the class implements an overloaded index operator

  20. 20 Main Index Contents Summary Slide 3 §- Two dimensional arrays in C++ - have the same problems as one-dimensional arrays: 1) fixed size 2) no size attribute 3) If it is a function argument, it is necessary to specify the number of columns as a constant.

More Related