Download presentation
Presentation is loading. Please wait.
1
Constant pointers and pointers to constants
If you have a value in your program and it should not change, or if you have a pointer and you don't want it to be reassigned to point to a different value, you should make it a constant with the const keyword.
2
Constant pointers and pointers to constants
There are generally two places that the const keyword can be used when declaring a pointer. Consider the following declaration: char ch = 'A'; char * chPtr = &ch; This is a simple declaration of the variable chPtr. chPtr is a pointer to a character variable and, in this case, points to the character 'A'.
3
Constant pointers and pointers to constants
Now consider the following three declarations assuming that ch has been defined as a type char variable (i.e. const char ch = 'A';) : const char * chPtr = &ch; char * const myPtr = &ch; const char * const chPtr = &ch; They are all three valid and correct declarations. Each assigns the address of ch to a character pointer. The difference is in what is constant.
4
Constant pointers and pointers to constants
The second declaration char * const myPtr declares a constant pointer to a character. The location stored in the pointer cannot change. You cannot change where this pointer points: char ch1 = 'A'; char ch2 = 'B'; char * const myPtr = &char_A; myPtr = &char_B; // error - can't change // address of myPtr
5
Constant pointers and pointers to constants
The third declares a pointer to a character where both the pointer value and the value being pointed to will not change. const char * const chPtr = &ch;
6
Pointers to Constant Must use const keyword in pointer definition:
const double taxRates[] = {0.65, 0.8, 0.75}; const double *ratePtr; Use const keyword for pointers in function headers to protect data from modification from within function
7
Pointer to Constant – What does the Definition Mean?
See pr10-13.cpp Read as: “rates is a pointer to a constant that is a double.”
8
Constant Pointers Defined with const keyword adjacent to variable name: int classSize = 24; int * const classPtr = &classSize; Must be initialized when defined Can be used without initialization as a function parameter Initialized by argument when function is called Function can receive different arguments on different calls
9
Constant Pointers While the address in the pointer cannot change, the data at that address may be changed
10
Constant Pointer to Constant
Can combine pointer to constants and constant pointers: int size = 10; const int * const ptr = &size; What does it mean?
11
Dynamic Memory Allocation
Can allocate storage for a variable while program is running Uses new operator to allocate memory double *dptr; dptr = new double; new returns address of memory location
12
Dynamic Memory Allocation
Can also use new to allocate array arrayPtr = new double[25]; Program may terminate if there is not sufficient memory Can then use [ ] or pointer arithmetic to access array
13
Dynamic Memory Example
int *count, *arrayptr; count = new int; cout <<"How many students? "; cin >> *count; arrayptr = new int[*count]; for (int i=0; i<*count; i++) { cout << "Enter score " << i << ": "; cin >> arrayptr[i]; }
14
Releasing Dynamic Memory
Use delete to free dynamic memory delete count; Use delete [] to free dynamic array memory delete [] arrayptr; Only use delete with dynamic memory! See pr10-14.cpp
15
Returning Pointers from Functions
Pointer can be the return type of function int* newNum(); The function must not return a pointer to a local variable in the function The function should only return a pointer to data that was passed to the function as an argument to dynamically allocated memory See pr10-15.cpp
16
Pointers to Class Objects
Can create pointers to objects and structure variables class Square {…}; Square sq1[4]; Square *squarePtr = &sq1[0];
17
Structure Pointer Operator
To access a member, use the form ptr->member: squarePtr->setSide(14); in place of the form (*ptr).member: (*squarePtr).setSide(14);
18
Dynamic Memory with Objects
Can allocate dynamic objects using pointers: stuPtr = new Student; Can pass values to constructor: squarePtr = new Square(17); delete causes destructor to be invoked: delete squarePtr; See pr10-16.cpp
19
Object Pointers as Function Parameters
Pointers to objects can be passed as parameters to functions Such pointers provide a pass-by-reference parameter mechanism Pointers must be dereferenced in the function to access the member fields See pr10-17.cpp
20
Controlling Memory Leaks
Memory that is allocated with new should be deallocated with a call to delete as soon as the memory is no longer needed. This is best done in the same function as the one that allocated the memory. For dynamically-created objects, new should be used in the constructor and delete should be used in the destructor See pr10-18cpp
21
Selecting Members of Objects
Situation: An object contains a pointer as a member. There is also a pointer to the object. Problem: How do we access the pointer member via the object pointer? class GradeList { private: string courseNum; int * grades; } GradeList test1, *test2 = new GradeList;
22
Selecting Members of Objects
Expression Meaning test1->grades Access the grades pointer in test1. This is the same as (*test1).grades *test1->grades Access the value pointed at by tes1->grades. This is the same as *(*test1).grades *test1.grades Access the value pointed at by test1.grades
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.