Chapter 15-1 Pointers, Dynamic Data, and Reference Types
description
Transcript of Chapter 15-1 Pointers, Dynamic Data, and Reference Types
![Page 1: Chapter 15-1 Pointers, Dynamic Data, and Reference Types](https://reader035.fdocuments.in/reader035/viewer/2022081513/5681683f550346895dde0d4d/html5/thumbnails/1.jpg)
1
Chapter 15-1
Pointers, Dynamic Data, and Reference
Types
Dale/Weems
![Page 2: Chapter 15-1 Pointers, Dynamic Data, and Reference Types](https://reader035.fdocuments.in/reader035/viewer/2022081513/5681683f550346895dde0d4d/html5/thumbnails/2.jpg)
2
Chapter 15 Topics Using the Address-Of Operator & Declaring and Using Pointer Variables Using the Indirection (Dereference) Operator * The NULL Pointer Using C++ Operators new and delete Meaning of an Inaccessible Object Meaning of a Dangling Pointer Use of a Class Destructor Shallow Copy vs. Deep Copy of Class Objects Use of a Copy Constructor
![Page 3: Chapter 15-1 Pointers, Dynamic Data, and Reference Types](https://reader035.fdocuments.in/reader035/viewer/2022081513/5681683f550346895dde0d4d/html5/thumbnails/3.jpg)
3
Recall that . . .char str [ 8 ];
str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant because the value of str itself cannot be changed by assignment. It “points” to the memory location of a char.
str [0] [1] [2] [3] [4] [5] [6] [7]
‘H’ ‘e’ ‘l’ ‘l’ ‘o’ ‘\0’
6000
![Page 4: Chapter 15-1 Pointers, Dynamic Data, and Reference Types](https://reader035.fdocuments.in/reader035/viewer/2022081513/5681683f550346895dde0d4d/html5/thumbnails/4.jpg)
4
Addresses in Memory When a variable is declared, enough memory to
hold a value of that type is allocated for it at an unused memory location. This is the address of the variable
int x; float number; char ch;
2000 2002 2006
x number ch
![Page 5: Chapter 15-1 Pointers, Dynamic Data, and Reference Types](https://reader035.fdocuments.in/reader035/viewer/2022081513/5681683f550346895dde0d4d/html5/thumbnails/5.jpg)
5
Obtaining Memory Addresses the address of a non-array variable can be obtained by
using the address-of operator &
int x;float number;char ch;
cout << “Address of x is “ << &x << endl;
cout << “Address of number is “ << &number << endl;
cout << “Address of ch is “ << &ch << endl;
![Page 6: Chapter 15-1 Pointers, Dynamic Data, and Reference Types](https://reader035.fdocuments.in/reader035/viewer/2022081513/5681683f550346895dde0d4d/html5/thumbnails/6.jpg)
6
What is a pointer variable? A pointer variable is a variable whose value is the
address of a location in memory To declare a pointer variable, you specify the type of
value that the pointer will point to, for example
int* ptr; // ptr will hold the address of an int
char* q; // q will hold the address of a char
![Page 7: Chapter 15-1 Pointers, Dynamic Data, and Reference Types](https://reader035.fdocuments.in/reader035/viewer/2022081513/5681683f550346895dde0d4d/html5/thumbnails/7.jpg)
7
Using a Pointer Variable
int x; x = 12;
int* ptr; ptr = &x;
NOTE: Because ptr holds the address of x, we say that ptr “points to” x
2000
12
x
3000
2000
ptr
![Page 8: Chapter 15-1 Pointers, Dynamic Data, and Reference Types](https://reader035.fdocuments.in/reader035/viewer/2022081513/5681683f550346895dde0d4d/html5/thumbnails/8.jpg)
8
2000
12
x
3000
2000
ptr
int x; x = 12;
int* ptr; ptr = &x;
cout << *ptr;
NOTE: The value pointed to by ptr is denoted by *ptr
Unary operator * is the indirection (deference) operator
![Page 9: Chapter 15-1 Pointers, Dynamic Data, and Reference Types](https://reader035.fdocuments.in/reader035/viewer/2022081513/5681683f550346895dde0d4d/html5/thumbnails/9.jpg)
9
int x; x = 12;
int* ptr; ptr = &x;
*ptr = 5; // Changes the value // at address ptr to 5
Using the Dereference Operator
2000
12 5
x
3000
2000
ptr
![Page 10: Chapter 15-1 Pointers, Dynamic Data, and Reference Types](https://reader035.fdocuments.in/reader035/viewer/2022081513/5681683f550346895dde0d4d/html5/thumbnails/10.jpg)
10
char ch; ch = ‘A’;
char* q; q = &ch;
*q = ‘Z’; char* p; p = q; // The rhs has value 4000 // Now p and q both point to ch
Another Example 4000
A Z
ch
5000 6000
4000 4000
q p
![Page 11: Chapter 15-1 Pointers, Dynamic Data, and Reference Types](https://reader035.fdocuments.in/reader035/viewer/2022081513/5681683f550346895dde0d4d/html5/thumbnails/11.jpg)
11
‘H’ ‘e’ ‘l’ ‘l’ ‘o’ ‘\0’
3000char msg[ ]=“Hello”; ‘M’ ‘a’
3001char* ptr; 3000
ptr = msg; // Recall that msg == // &msg[ 0 ]*ptr = ‘M’; ptr++; // Increments the address*ptr = ‘a’; // in ptr
ptr
Using a Pointer to Access the Elements of a String
![Page 12: Chapter 15-1 Pointers, Dynamic Data, and Reference Types](https://reader035.fdocuments.in/reader035/viewer/2022081513/5681683f550346895dde0d4d/html5/thumbnails/12.jpg)
12
int StringLength (/* in */ const char str[] ) // Precondition: str is a null-terminated string// Postcondition: Return value == length of str // (not counting ‘\0’){ char* p; int count = 0;
p = str;
while (*p != ‘\0’) { count++; p++; // Increments the address p by sizeof char }
return count;
}
12
![Page 13: Chapter 15-1 Pointers, Dynamic Data, and Reference Types](https://reader035.fdocuments.in/reader035/viewer/2022081513/5681683f550346895dde0d4d/html5/thumbnails/13.jpg)
13
Indexing a pointer
These two parameters are the same char str[] char* str Indexing a pointer is allowed (whether
the pointer points to an array or not) Indexing is valid for any pointer
expression
![Page 14: Chapter 15-1 Pointers, Dynamic Data, and Reference Types](https://reader035.fdocuments.in/reader035/viewer/2022081513/5681683f550346895dde0d4d/html5/thumbnails/14.jpg)
14
C++ Data TypesC++ Data Types
structured
array struct union class
address
pointer reference
simple
integral enum
char short int long bool
floating
float double long double
![Page 15: Chapter 15-1 Pointers, Dynamic Data, and Reference Types](https://reader035.fdocuments.in/reader035/viewer/2022081513/5681683f550346895dde0d4d/html5/thumbnails/15.jpg)
Some C++ Pointer OperationsPrecedence Higher -> Select member of class pointed to
Unary: ++ -- ! * & new delete Increment, Decrement, NOT, Dereference, Address-of, Allocate, Deallocate
Binary: + - Add Subtract
< <= > >= Relational operators
== != Tests for equality, inequality
Lower = Assignment15
![Page 16: Chapter 15-1 Pointers, Dynamic Data, and Reference Types](https://reader035.fdocuments.in/reader035/viewer/2022081513/5681683f550346895dde0d4d/html5/thumbnails/16.jpg)
Operator new Syntax
new DataType
new DataType [IntExpression]
If memory is available in an area called the heap (or free store) new allocates space for the requested object or array and returns a pointer to (address of) the memory allocated
Otherwise, program terminates with error message
The dynamically allocated object exists until the delete operator destroys it
16
![Page 17: Chapter 15-1 Pointers, Dynamic Data, and Reference Types](https://reader035.fdocuments.in/reader035/viewer/2022081513/5681683f550346895dde0d4d/html5/thumbnails/17.jpg)
17
The NULL Pointer
NULL is a pointer constant 0, defined in header file cstddef, that means that the pointer points to nothing
It is an error to dereference a pointer whose value is NULL
Such an error may cause your program to crash, or behave erratically while (ptr != NULL)
{ . . . // Ok to use *ptr here }
![Page 18: Chapter 15-1 Pointers, Dynamic Data, and Reference Types](https://reader035.fdocuments.in/reader035/viewer/2022081513/5681683f550346895dde0d4d/html5/thumbnails/18.jpg)
18
3 Kinds of Program Data Static data: memory allocation exists throughout
execution of program static long currentSeed;
Automatic data: automatically created at function entry, resides in activation frame of the function, and is destroyed when returning from function
Dynamic data: explicitly allocated and deallocated during program execution by C++ instructions written by programmer using operators new and delete
![Page 19: Chapter 15-1 Pointers, Dynamic Data, and Reference Types](https://reader035.fdocuments.in/reader035/viewer/2022081513/5681683f550346895dde0d4d/html5/thumbnails/19.jpg)
19
Allocation of Memory
STATIC ALLOCATION
Static allocation is the allocation of memory space at compile time
DYNAMIC ALLOCATION
Dynamic allocation is the allocation of memory space at run time by using operator new
![Page 20: Chapter 15-1 Pointers, Dynamic Data, and Reference Types](https://reader035.fdocuments.in/reader035/viewer/2022081513/5681683f550346895dde0d4d/html5/thumbnails/20.jpg)
20
2000
ptr
Dynamically Allocated Data
char* ptr;
ptr = new char;
*ptr = ‘B’;
cout << *ptr;
![Page 21: Chapter 15-1 Pointers, Dynamic Data, and Reference Types](https://reader035.fdocuments.in/reader035/viewer/2022081513/5681683f550346895dde0d4d/html5/thumbnails/21.jpg)
21
Dynamically Allocated Datachar* ptr;
ptr = new char;
*ptr = ‘B’;
cout << *ptr;
NOTE: Dynamic data has no variable name
2000
ptr
![Page 22: Chapter 15-1 Pointers, Dynamic Data, and Reference Types](https://reader035.fdocuments.in/reader035/viewer/2022081513/5681683f550346895dde0d4d/html5/thumbnails/22.jpg)
22
Dynamically Allocated Datachar* ptr;
ptr = new char;
*ptr = ‘B’;
cout << *ptr;
NOTE: Dynamic data has no variable name
2000
ptr
‘B’
![Page 23: Chapter 15-1 Pointers, Dynamic Data, and Reference Types](https://reader035.fdocuments.in/reader035/viewer/2022081513/5681683f550346895dde0d4d/html5/thumbnails/23.jpg)
23
Dynamically Allocated Data
char* ptr;
ptr = new char;
*ptr = ‘B’;
cout << *ptr;
delete ptr;
2000
ptr
NOTE: delete deallocates the memory pointed to by ptr
?
![Page 24: Chapter 15-1 Pointers, Dynamic Data, and Reference Types](https://reader035.fdocuments.in/reader035/viewer/2022081513/5681683f550346895dde0d4d/html5/thumbnails/24.jpg)
Operator delete returns memory to the free store, which was previously allocated at run-time by operator new
The object or array currently pointed to by the pointer is deallocated, and the pointer is considered unassigned
Using Operator delete
24
![Page 25: Chapter 15-1 Pointers, Dynamic Data, and Reference Types](https://reader035.fdocuments.in/reader035/viewer/2022081513/5681683f550346895dde0d4d/html5/thumbnails/25.jpg)
25
Dynamic Array Allocation char *ptr;// ptr is a pointer variable that // can hold the address of a charptr = new char[ 5 ]; // Allocates memory for a 5-character array // dynamically at run time and stores the// base address into ptr
ptr
6000
6000
![Page 26: Chapter 15-1 Pointers, Dynamic Data, and Reference Types](https://reader035.fdocuments.in/reader035/viewer/2022081513/5681683f550346895dde0d4d/html5/thumbnails/26.jpg)
26
Dynamic Array Allocation char *ptr;
ptr = new char[ 5 ]; strcpy(ptr, “Bye”);
ptr[ 1 ] = ‘u’;// A pointer can be subscriptedcout << ptr[ 2];
ptr
6000
6000 ‘B’ ‘y’ ‘e’ ‘\0’ ‘u’
![Page 27: Chapter 15-1 Pointers, Dynamic Data, and Reference Types](https://reader035.fdocuments.in/reader035/viewer/2022081513/5681683f550346895dde0d4d/html5/thumbnails/27.jpg)
Operator delete Syntax delete Pointer
delete [ ] Pointer
If the value of the pointer is NULL there is no effect
Otherwise, the object or array currently pointed to by Pointer is deallocated, and the value of Pointer is undefined
The memory is returned to the free store
Square brackets are used with delete to deallocate a dynamically allocated array
27
![Page 28: Chapter 15-1 Pointers, Dynamic Data, and Reference Types](https://reader035.fdocuments.in/reader035/viewer/2022081513/5681683f550346895dde0d4d/html5/thumbnails/28.jpg)
28
Dynamic Array Deallocation char *ptr;ptr = new char[ 5 ]; strcpy(ptr, “Bye”);ptr[ 1 ] = ‘u’;delete [] ptr; // Deallocates array pointed to by ptr// ptr itself is not deallocated// The value of ptr is undefined
ptr
?
![Page 29: Chapter 15-1 Pointers, Dynamic Data, and Reference Types](https://reader035.fdocuments.in/reader035/viewer/2022081513/5681683f550346895dde0d4d/html5/thumbnails/29.jpg)
29
The End of Chapter 15 Part 1