Download presentation
Presentation is loading. Please wait.
1
Main Index Contents 11 Main Index Contents Pointer Illustration Pointer Illustration Vertical / Horizontal View. Vertical / Horizontal View. Data Addresses in Memory Data Addresses in Memory Declaring Pointer Variables Declaring Pointer Variables Assigning Values to Pointers Assigning Values to Pointers Accessing Data with Pointers Accessing Data with Pointers Arrays and Pointers Arrays and Pointers Operator ‘new’ Operator ‘new’ Operator ‘delete’ Operator ‘delete’ Illustrating the Destructor Illustrating the Destructor Copy Constructor / Overloaded Assignment Operator Copy Constructor / Overloaded Assignment Operator Declaration of dynamicClass Objects Declaration of dynamicClass Objects Chapter 5 – Pointers and Dynamic Memory The Pointer ‘this’ The Pointer ‘this’ dynamicClass Copy Constructor dynamicClass Copy Constructor The C++ Index Operator [] The C++ Index Operator [] Matrices Summary Slides Summary Slides Summary Slides Summary Slides (3 pages)
2
Main Index Contents 22 Main Index Contents Pointer Illustration
3
Main Index Contents 33 Main Index Contents Vertical and Horizontal View of Memory
4
Main Index Contents 44 Main Index Contents Data Addresses in Memory
5
Main Index Contents 55 Main Index Contents 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. Declaring Pointer Variables int *intPtr; char *charPtr; type *ptr;
6
Main Index Contents 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. intPtr = &m; int m = 50, *intPtr;
7
Main Index Contents 77 Main Index Contents Accessing Data with Pointers int x = 50, y = 100, *px = &x, *py = &y;
8
Main Index Contents 88 Main Index Contents Arrays and Pointers
9
Main Index Contents 99 Main Index Contents Operator ‘new’ p = new time24; // *p is 00:00 (midnight) q = new time24(8, 15); // *q is 8:15 AM
10
Main Index Contents 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
Main Index Contents 11 Main Index Contents Illustrating the Destructor
12
Main Index Contents 12 Copy Constructor / Overloaded Assignment Operator Initialization and Assignment both have the effect of producing a data item that is a copy of an existing item
13
Main Index Contents 13 Main Index Contents Declaration of dynamicClass Objects The byte-by-byte copy performed by default by C++ is inappropriate for dynamic classes The copy of an objA in ObjB makes two pointers point at the same memory location. This creates a memory leak when one of the two objects is deleted.
14
Main Index Contents 14 The Pointer ‘this’ *thisobjA; this-> memberl
15
Main Index Contents 15 Main Index Contents dynamicClass Copy Constructor Algorithm Example: dynamicClass obj(3,5), objB=objA;
16
Main Index Contents 16 The MiniVector Check the code of the MiniVector – A small version of the Vector Class available in STL The MiniVector uses a function called Reserve() to allocate a dynamic array for extra space
17
Main Index Contents 17 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]
18
Main Index Contents 18 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.
19
Main Index Contents 19 Main Index Contents Vectors and Matrices A dynamic Matrix is represented by using the Vector Class. – Visit the matrix.h file A dynamic Matrix can be described by using dynamic arrays as well. The code is more complex than using vectors. ( Think!) - Food for you brain – How would you define a dynamic matrix by using a dynamic array?
20
Main Index Contents 20 Main Index Contents Summary Slide 1 §- Pointers contain 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.
21
Main Index Contents 21 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
22
Main Index Contents 22 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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.