Chapter 5 Pointers and Dynamic Memory Pointer Illustration
















![The C++ Index Operator [ ] arr[i] = 30; // arr[i] is the address The C++ Index Operator [ ] arr[i] = 30; // arr[i] is the address](https://slidetodoc.com/presentation_image_h/283dd1cccff962e5563b21c2cc706d94/image-17.jpg)





- Slides: 22
Chapter 5 – Pointers and Dynamic Memory 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 dynamic. Class Objects 1 Main Index The Pointer ‘this’ dynamic. Class Copy Constructor The C++ Index Operator [] Matrices Summary Slides (3 pages) Contents
Pointer Illustration 2 Main Index Contents
Vertical and Horizontal View of Memory 3 Main Index Contents
Data Addresses in Memory 4 Main Index Contents
l Declaring Pointer Variables Declare a pointer by stating the type followed by the variable name, but with a "*" added immediately before the name. type *ptr; l The pointer ptr is a variable whose value is the address of a data item of the designated type. int *int. Ptr; char *char. Ptr; 5 Main Index Contents
Assigning Values to Pointers int m = 50, *int. Ptr; l &m is the address of the integer in memory. The assignment statement int. Ptr = &m; 6 l sets int. Ptr to point at an actual data item. l The Figure illustrates the status of the two variables from the declaration statement and the resulting status after the assignment of a value to int. Ptr. Main Index Contents
Accessing Data with Pointers int x = 50, y = 100, *px = &x, *py = &y; 7 Main Index Contents
Arrays and Pointers 8 Main Index Contents
Operator ‘new’ p = new time 24; // *p is 00: 00 (midnight) q = new time 24(8, 15); // *q is 8: 15 AM 9 Main Index Contents
Operator ‘delete’ l 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 10 Main Index Contents
Illustrating the Destructor 11 Main Index Contents
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 12 Main Index Contents
Declaration of dynamic. Class Objects • The byte-by-byte copy performed by default by C++ is inappropriate for dynamic classes • The copy of an obj. A in Obj. B makes two pointers point at the same memory location. • This creates a memory leak when one of the two objects is deleted. 13 Main Index Contents
The Pointer ‘this’ *this obj. A; this-> 14 memberl Main Index Contents
dynamic. Class Copy Constructor Algorithm Example: dynamic. Class<int> obj(3, 5), obj. B=obj. A; 15 Main Index Contents
The Mini. Vector l Check the code of the Mini. Vector – l 16 A small version of the Vector Class available in STL The Mini. Vector uses a function called Reserve() to allocate a dynamic array for extra space Main Index Contents
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 Main Index Contents
l l 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 Main Index Contents
Vectors and Matrices l A dynamic Matrix is represented by using the Vector Class. – l 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? 19 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. 20 Main Index Contents
Summary Slide 2 §- vector implementation - The mini. Vector 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 21 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. 22 Main Index Contents






















