SlideServe
Menu
Préférences
© SlideServe · v2026

Main Index

kylia avatar
20 diapositives · 355 Views · Sep 17, 2014

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’

Main Index - slide 1
01 / 20Main Index
1 / 17

Télécharger la présentation

Main Index
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  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.