Lecture 2 Pointers Pointers with Arrays Dynamic Memory Allocation.
Chapter 15-2 Pointers, Dynamic Data, and Reference Types
description
Transcript of Chapter 15-2 Pointers, Dynamic Data, and Reference Types
![Page 1: Chapter 15-2 Pointers, Dynamic Data, and Reference Types](https://reader035.fdocuments.net/reader035/viewer/2022062314/56813e7a550346895da89f61/html5/thumbnails/1.jpg)
1
Chapter 15-2
Pointers, Dynamic Data, and Reference
Types
Dale/Weems
![Page 2: Chapter 15-2 Pointers, Dynamic Data, and Reference Types](https://reader035.fdocuments.net/reader035/viewer/2022062314/56813e7a550346895da89f61/html5/thumbnails/2.jpg)
2
int* ptr = new int; *ptr = 3;
ptr = new int; // Changes value of ptr *ptr = 4;
What happens here?
3
ptr
3
ptr
4
![Page 3: Chapter 15-2 Pointers, Dynamic Data, and Reference Types](https://reader035.fdocuments.net/reader035/viewer/2022062314/56813e7a550346895da89f61/html5/thumbnails/3.jpg)
3
Inaccessible Object
An inaccessible object is an unnamed object created by operator new that a programmer has left without a pointer to it.
int* ptr = new int; *ptr = 8; int* ptr2 = new int; *ptr2 = -5;
How else can an object become inaccessible?
8
ptr
-5
ptr2
![Page 4: Chapter 15-2 Pointers, Dynamic Data, and Reference Types](https://reader035.fdocuments.net/reader035/viewer/2022062314/56813e7a550346895da89f61/html5/thumbnails/4.jpg)
4
Making an Object Inaccessible
int* ptr = new int; *ptr = 8; int* ptr2 = new int; *ptr2 = -5;
ptr = ptr2; //Here the 8 becomes
// inaccessible 8
ptr
-5
ptr2
8
ptr
-5
ptr2
![Page 5: Chapter 15-2 Pointers, Dynamic Data, and Reference Types](https://reader035.fdocuments.net/reader035/viewer/2022062314/56813e7a550346895da89f61/html5/thumbnails/5.jpg)
5
Memory Leak
A memory leak is the loss of available memory space that occurs when dynamic data is allocated but never deallocated
![Page 6: Chapter 15-2 Pointers, Dynamic Data, and Reference Types](https://reader035.fdocuments.net/reader035/viewer/2022062314/56813e7a550346895da89f61/html5/thumbnails/6.jpg)
6
A dangling pointer is a pointer that points to dynamic memory that has been deallocated
int* ptr = new int; *ptr = 8; int* ptr2 = new int; *ptr2 = -5; ptr = ptr2;
A Dangling Pointer
8
ptr
-5
ptr2
For example,
![Page 7: Chapter 15-2 Pointers, Dynamic Data, and Reference Types](https://reader035.fdocuments.net/reader035/viewer/2022062314/56813e7a550346895da89f61/html5/thumbnails/7.jpg)
7
int* ptr = new int; *ptr = 8; int* ptr2 = new int; *ptr2 = -5; ptr = ptr2;
delete ptr2; // ptr is left dangling
ptr2 = NULL;
Leaving a Dangling Pointer
8
ptr
-5
ptr2
8
ptr
NULL
ptr2
![Page 8: Chapter 15-2 Pointers, Dynamic Data, and Reference Types](https://reader035.fdocuments.net/reader035/viewer/2022062314/56813e7a550346895da89f61/html5/thumbnails/8.jpg)
8
Reference Types
int& intRef;
Reference Type Declaration
• DataType& Vaiable;• DataType &Variable, &Variable, …;
![Page 9: Chapter 15-2 Pointers, Dynamic Data, and Reference Types](https://reader035.fdocuments.net/reader035/viewer/2022062314/56813e7a550346895da89f61/html5/thumbnails/9.jpg)
9
Use reference variable
Use reference variable as alias
• int someInt;
int& intRef = someInt;
intRef = 1;• void DatePrint(Date& someDate) {
someDate.Print();
}
![Page 10: Chapter 15-2 Pointers, Dynamic Data, and Reference Types](https://reader035.fdocuments.net/reader035/viewer/2022062314/56813e7a550346895da89f61/html5/thumbnails/10.jpg)
10
The difference of using a reference variable and using a pointer variable
Using a Reference Variable
int gamma = 26;
int& intRef = gamma;
// Assert: intRef points to gamma
intRef = 35;
// Assert: gamma == 35
intRef = intRef + 3;
// Assert: gamma == 38
Using a Pointer Variable
int gamma = 26;
int* intPtr = γ
// Assert: intPtr points to gamma
*intPtr = 35;
// Assert: gamma == 35
*intPtr = *intPtr + 3;
// Assert: gamma == 38
![Page 11: Chapter 15-2 Pointers, Dynamic Data, and Reference Types](https://reader035.fdocuments.net/reader035/viewer/2022062314/56813e7a550346895da89f61/html5/thumbnails/11.jpg)
11
Reference Variables are Constant Pointers
Reference variables cannot be reassigned after initialization
Initialization means:• Explicit initialization in a declaration• Implicit initialization by passing an argument
to a parameter• Implicit initialization by returning a function
value
![Page 12: Chapter 15-2 Pointers, Dynamic Data, and Reference Types](https://reader035.fdocuments.net/reader035/viewer/2022062314/56813e7a550346895da89f61/html5/thumbnails/12.jpg)
12
The use of reference variables
The following code fails:
void Swap(float x, float y) {float temp = x;
x = y;y = temp;
}
Swap(alpha, beta);
![Page 13: Chapter 15-2 Pointers, Dynamic Data, and Reference Types](https://reader035.fdocuments.net/reader035/viewer/2022062314/56813e7a550346895da89f61/html5/thumbnails/13.jpg)
13
The use of reference variables
The following code uses pointer variables:
void Swap(float* x, float* y) {float temp = *x;
*x = *y;*y = temp;
}
Swap(&alpha, &beta);
![Page 14: Chapter 15-2 Pointers, Dynamic Data, and Reference Types](https://reader035.fdocuments.net/reader035/viewer/2022062314/56813e7a550346895da89f61/html5/thumbnails/14.jpg)
14
The use of reference variables
The following code uses reference variables:
void Swap(float& x, float& y) {float temp = x;
x = y;y = temp;
}
Swap(alpha, beta);
![Page 15: Chapter 15-2 Pointers, Dynamic Data, and Reference Types](https://reader035.fdocuments.net/reader035/viewer/2022062314/56813e7a550346895da89f61/html5/thumbnails/15.jpg)
15
The usage of Ampersand (&)Position Usage Meaning
Prefix &Variable Address of operation
Infix Expression & Expression
Bitwise AND operation
Infix Expression && Expression
Logical AND operation
Postfix DataType& DataType (specifically, a reference type)
Exception: To declare two variables of reference types, the & must be attached to each variable name:
Int &var1, &var2;
![Page 16: Chapter 15-2 Pointers, Dynamic Data, and Reference Types](https://reader035.fdocuments.net/reader035/viewer/2022062314/56813e7a550346895da89f61/html5/thumbnails/16.jpg)
16
// Specification file (“dynarray.h”) // Safe integer array class allows run-time specification // of size, prevents indexes from going out of bounds, // allows aggregate array copying and initialization
class DynArray {public: DynArray(/* in */ int arrSize); // Constructor
// PRE: arrSize is assigned// POST: IF arrSize >= 1 && enough memory THEN // Array of size arrSize is created with // all elements == 0 ELSE error message
DynArray(const DynArray& otherArr);// Copy constructor// POST: this DynArray is a deep copy of
otherArr// Is implicitly called for initialization
![Page 17: Chapter 15-2 Pointers, Dynamic Data, and Reference Types](https://reader035.fdocuments.net/reader035/viewer/2022062314/56813e7a550346895da89f61/html5/thumbnails/17.jpg)
17
// Specification file continued
~DynArray();
// Destructor
// POST: Memory for dynamic array deallocated
int ValueAt (/* in */ int i) const;
// PRE: i is assigned
// POST: IF 0 <= i < size of this array THEN
// FCTVAL == value of array element at index i
// ELSE error message
void Store (/* in */ int val, /* in */ int i)
// PRE: val and i are assigned
// POST: IF 0 <= i < size of this array THEN
// val is stored in array element i
// ELSE error message
17
![Page 18: Chapter 15-2 Pointers, Dynamic Data, and Reference Types](https://reader035.fdocuments.net/reader035/viewer/2022062314/56813e7a550346895da89f61/html5/thumbnails/18.jpg)
18
// Specification file continued
void CopyFrom (/* in */ DynArray otherArr);// POST: IF enough memory THEN
// new array created (as deep copy) // with size and contents // same as otherArr
// ELSE error message.
private: int* arr; int size;
};
18
![Page 19: Chapter 15-2 Pointers, Dynamic Data, and Reference Types](https://reader035.fdocuments.net/reader035/viewer/2022062314/56813e7a550346895da89f61/html5/thumbnails/19.jpg)
19
class DynArray
80
40
90 ? ?
Private data:
size 5
arr 6000
Free store
6000DynArray
Store
ValueAt
DynArray
~DynArray
CopyFrom
![Page 20: Chapter 15-2 Pointers, Dynamic Data, and Reference Types](https://reader035.fdocuments.net/reader035/viewer/2022062314/56813e7a550346895da89f61/html5/thumbnails/20.jpg)
20
DynArray beta(5); //constructor
?
?
? ? ?
Private data:
size 5
arr 2000
Free store
2000DynArray
Store
ValueAt
DynArray
~DynArray
CopyFrom
beta
![Page 21: Chapter 15-2 Pointers, Dynamic Data, and Reference Types](https://reader035.fdocuments.net/reader035/viewer/2022062314/56813e7a550346895da89f61/html5/thumbnails/21.jpg)
21
DynArray::DynArray(/* in */ int arrSize)
// Constructor// PRE: arrSize is assigned// POST: IF arrSize >= 1 && enough memory THEN // Array of size arrSize is created with // all elements == 0 ELSE error message
{ int i; if (arrSize < 1) { cerr << “DynArray constructor - invalid size: “
<< arrSize << endl; exit(1);
}
arr = new int[arrSize]; // Allocate memory
size = arrSize;
for (i = 0; i < size; i++) arr[i] = 0;
} 21
![Page 22: Chapter 15-2 Pointers, Dynamic Data, and Reference Types](https://reader035.fdocuments.net/reader035/viewer/2022062314/56813e7a550346895da89f61/html5/thumbnails/22.jpg)
22
beta.Store(75, 2);
?
?
75 ? ?
Private data:
size 5
arr 2000
Free store
2000DynArray
Store
ValueAt
DynArray
~DynArray
CopyFrom
beta
![Page 23: Chapter 15-2 Pointers, Dynamic Data, and Reference Types](https://reader035.fdocuments.net/reader035/viewer/2022062314/56813e7a550346895da89f61/html5/thumbnails/23.jpg)
23
void DynArray::Store (/* in */ int val, /* in */ int i) // PRE: val and i are assigned// POST: IF 0 <= i < size of this array THEN// arr[i] == val// ELSE error message
{
if (i < 0 || i >= size) {
cerr << “Store - invalid index : “ << i << endl; exit(1);
}
arr[i] = val;
}
23
![Page 24: Chapter 15-2 Pointers, Dynamic Data, and Reference Types](https://reader035.fdocuments.net/reader035/viewer/2022062314/56813e7a550346895da89f61/html5/thumbnails/24.jpg)
24
?
?
? ?
Private:
size 4
arr 3000
3000 Private:
size 5
arr 2000
2000
?
?
75 ? ?
gamma beta
DynArray gamma(4);//Constructor
DynArray
Store
ValueAt
DynArray
~DynArray
CopyFrom
DynArray
Store
ValueAt
DynArray
~DynArray
CopyFrom
![Page 25: Chapter 15-2 Pointers, Dynamic Data, and Reference Types](https://reader035.fdocuments.net/reader035/viewer/2022062314/56813e7a550346895da89f61/html5/thumbnails/25.jpg)
25
?
?
-8 ?
Private:
size 4
arr 3000
3000 Private:
size 5
arr 2000
2000
?
?
75 ? ?
gamma beta
gamma.Store(-8,2);
DynArray
Store
ValueAt
DynArray
~DynArray
CopyFrom
DynArray
Store
ValueAt
DynArray
~DynArray
CopyFrom
![Page 26: Chapter 15-2 Pointers, Dynamic Data, and Reference Types](https://reader035.fdocuments.net/reader035/viewer/2022062314/56813e7a550346895da89f61/html5/thumbnails/26.jpg)
26
int DynArray::ValueAt (/* in */ int i) const // PRE: i is assigned// POST: IF 0 <= i < size THEN// Return value == arr[i] // ELSE halt with error message
{ if (i < 0 || i >= size) { cerr << “ValueAt - invalid index : “ << i << endl; exit(1); } return arr[i];}
26
![Page 27: Chapter 15-2 Pointers, Dynamic Data, and Reference Types](https://reader035.fdocuments.net/reader035/viewer/2022062314/56813e7a550346895da89f61/html5/thumbnails/27.jpg)
27
Why is a destructor needed?
When a DynArray class variable goes out of scope, the memory space for data members size and pointer arr is deallocated
But the dynamic array that arr points to is not automatically deallocated
A class destructor is used to deallocate the dynamic memory pointed to by the data member
![Page 28: Chapter 15-2 Pointers, Dynamic Data, and Reference Types](https://reader035.fdocuments.net/reader035/viewer/2022062314/56813e7a550346895da89f61/html5/thumbnails/28.jpg)
28
DynArray::~DynArray() // Destructor// POST: Memory for dynamic array deallocated
{ delete [ ] arr;}
28
class DynArray Destructor
![Page 29: Chapter 15-2 Pointers, Dynamic Data, and Reference Types](https://reader035.fdocuments.net/reader035/viewer/2022062314/56813e7a550346895da89f61/html5/thumbnails/29.jpg)
29
The End of Chapter 15 Part 2