Pointers
description
Transcript of Pointers
![Page 1: Pointers](https://reader035.fdocuments.net/reader035/viewer/2022062519/56815126550346895dbf4467/html5/thumbnails/1.jpg)
Pointers
ACS 169
![Page 2: Pointers](https://reader035.fdocuments.net/reader035/viewer/2022062519/56815126550346895dbf4467/html5/thumbnails/2.jpg)
Pointers Pointer
a data type stores a memory address points to whatever the memory location
contains
A pointer is a variable that can store a memory address. The type of item pointed to by a pointer
variable is the target type.
![Page 3: Pointers](https://reader035.fdocuments.net/reader035/viewer/2022062519/56815126550346895dbf4467/html5/thumbnails/3.jpg)
Examples
Declaring Pointerstype *name;
Pointer Declarations: int * ptr; char *cptr; double *dptr;
![Page 4: Pointers](https://reader035.fdocuments.net/reader035/viewer/2022062519/56815126550346895dbf4467/html5/thumbnails/4.jpg)
Pointers int * ptr;
int v = 5;ptr = & v ;
The & operator is called the address operator
either of the following assignments will store the value of 10 in the variable v:
v = 10 ; *ptr = 10 ;
![Page 5: Pointers](https://reader035.fdocuments.net/reader035/viewer/2022062519/56815126550346895dbf4467/html5/thumbnails/5.jpg)
Pointers: Dereferencing cont. So far the asterisk is used in two ways:
1. int *ptr; 2. *ptr = 10 ;
the first declares the variable ptr as a pointer of type integer but it does not make it point to any memory address.
the second assigns the value 10 to the memory location pointed at by ptr. dereferencing
![Page 6: Pointers](https://reader035.fdocuments.net/reader035/viewer/2022062519/56815126550346895dbf4467/html5/thumbnails/6.jpg)
Question Explain each line in the following code:
int *ptr ; int number = 42 ; ptr = &number ; *ptr = 0 ; cout << *ptr <<endl ; cout << number << endl;
![Page 7: Pointers](https://reader035.fdocuments.net/reader035/viewer/2022062519/56815126550346895dbf4467/html5/thumbnails/7.jpg)
![Page 8: Pointers](https://reader035.fdocuments.net/reader035/viewer/2022062519/56815126550346895dbf4467/html5/thumbnails/8.jpg)
Pointer Graph Representation
![Page 9: Pointers](https://reader035.fdocuments.net/reader035/viewer/2022062519/56815126550346895dbf4467/html5/thumbnails/9.jpg)
Dereferencing
![Page 10: Pointers](https://reader035.fdocuments.net/reader035/viewer/2022062519/56815126550346895dbf4467/html5/thumbnails/10.jpg)
Pointers cont. Explain the following code:
int *p1 ;int *p2;
p1 = & count ; int *p2;
*p2 = *p1; p2 = p1 ;
![Page 11: Pointers](https://reader035.fdocuments.net/reader035/viewer/2022062519/56815126550346895dbf4467/html5/thumbnails/11.jpg)
Quiz #2
![Page 12: Pointers](https://reader035.fdocuments.net/reader035/viewer/2022062519/56815126550346895dbf4467/html5/thumbnails/12.jpg)
Dynamic Variables Dynamic Variables (DVs) are different
than normal variables in two aspects:
1. DVs are not declared (have no identifiers, …no variable names)
2. DVs are created during execution phase of a program and not during compilation, the keyword new is used for this purpose.
![Page 13: Pointers](https://reader035.fdocuments.net/reader035/viewer/2022062519/56815126550346895dbf4467/html5/thumbnails/13.jpg)
Dynamic Variables cont.Example:
int * ptr; ptr = new int;
this statement creates a DV of type integer and uses ptr to point to it. There is no identifier (name) for the variable pointed at by ptr
![Page 14: Pointers](https://reader035.fdocuments.net/reader035/viewer/2022062519/56815126550346895dbf4467/html5/thumbnails/14.jpg)
Dynamic Variables cont. The creation of new DVs is called
memory allocation and the allocated memory is called dynamic memory
ptr = new int;
Makes ptr point to a newly allocated integer variable from dynamic memory.
![Page 15: Pointers](https://reader035.fdocuments.net/reader035/viewer/2022062519/56815126550346895dbf4467/html5/thumbnails/15.jpg)
Dynamic Variables cont.
int *ptr ; ptr = new int ;*ptr = 33;
![Page 16: Pointers](https://reader035.fdocuments.net/reader035/viewer/2022062519/56815126550346895dbf4467/html5/thumbnails/16.jpg)
Dynamic Variables cont.
int *n;n = new int(17);
MyType *p;p = new MyType;orp = new MyType(32.0, 17);
![Page 17: Pointers](https://reader035.fdocuments.net/reader035/viewer/2022062519/56815126550346895dbf4467/html5/thumbnails/17.jpg)
The new operator & Objects throttle *t_ptr;
t_ptr = new throttle(50) ; calls the throttle constructor with
an integer argument
![Page 18: Pointers](https://reader035.fdocuments.net/reader035/viewer/2022062519/56815126550346895dbf4467/html5/thumbnails/18.jpg)
Dynamic Arrays. To declare a dynamic array use: int *ptr ; ptr = new int[10] ;
the new operator allocates a dynamic array of 10 integers and makes ptr point to its first element.
![Page 19: Pointers](https://reader035.fdocuments.net/reader035/viewer/2022062519/56815126550346895dbf4467/html5/thumbnails/19.jpg)
Dynamic Arrays cont. the statement:
ptr[5] = 33;
will store the value 33 as the 6th element in the array pointed to by ptr
![Page 20: Pointers](https://reader035.fdocuments.net/reader035/viewer/2022062519/56815126550346895dbf4467/html5/thumbnails/20.jpg)
Heap When new allocates a dynamic
variable or dynamic array, the memory comes from a location called the program’s heap (also called the free store).
bad_alloc exception is thrown when new attempts to allocate memory and fails.
![Page 21: Pointers](https://reader035.fdocuments.net/reader035/viewer/2022062519/56815126550346895dbf4467/html5/thumbnails/21.jpg)
Question Determine what the following code will do
int array_size ; int *numbers; cout << “how many numbers do you
have?”; cin >> array_size ; numbers = new int[array_size] ;
![Page 22: Pointers](https://reader035.fdocuments.net/reader035/viewer/2022062519/56815126550346895dbf4467/html5/thumbnails/22.jpg)
Answer The operator new is used to
dynamically allocate an array of size array_size that the user enters interactively.
![Page 23: Pointers](https://reader035.fdocuments.net/reader035/viewer/2022062519/56815126550346895dbf4467/html5/thumbnails/23.jpg)
Question Who should initialize the
components of a dynamically allocated array whose components are of a class data type ?
Ans: the default constructor will initialize all components of the array
![Page 24: Pointers](https://reader035.fdocuments.net/reader035/viewer/2022062519/56815126550346895dbf4467/html5/thumbnails/24.jpg)
Delete Operator It is an efficient practice to release
any heap memory that is no longer needed.
The delete operator is used in C++ to release memory to the heap that is no longer needed.
![Page 25: Pointers](https://reader035.fdocuments.net/reader035/viewer/2022062519/56815126550346895dbf4467/html5/thumbnails/25.jpg)
Delete operation Examples:
int *ptr;ptr = new int;…delete ptr ;ptr = NULL;
int *p;p = new int[30];…delete [] p;
![Page 26: Pointers](https://reader035.fdocuments.net/reader035/viewer/2022062519/56815126550346895dbf4467/html5/thumbnails/26.jpg)
Pointers as value parametersint *main_ptr;main_ptr = new int;make_it_42(main_ptr);
void make_it_42(int* my_ptr) ; { *my_ptr = 42;}
![Page 27: Pointers](https://reader035.fdocuments.net/reader035/viewer/2022062519/56815126550346895dbf4467/html5/thumbnails/27.jpg)
Pointers as value parameters The following function prototype:
void make_it_42(int* my_ptr) ;
the int* indicates that the parameter is of data type integer pointer.
the parameter is a value parameter because of the absence of the & operator.
![Page 28: Pointers](https://reader035.fdocuments.net/reader035/viewer/2022062519/56815126550346895dbf4467/html5/thumbnails/28.jpg)
Array Parametersvoid make_it_all_42( double * num, size_t n);….double *numbers;num = new double[10];make_it_all_42(num, 10);….void make_it_all_42(double * num, int n){ for(int i=0; i<n; i++)
num[i] = 42;}
![Page 29: Pointers](https://reader035.fdocuments.net/reader035/viewer/2022062519/56815126550346895dbf4467/html5/thumbnails/29.jpg)
void make_it_all_42(double * num, int n){ for(size_t i=0; i<n; i++)
num[i] = 42;}
void make_it_all_42(double num[], int n){ for(size_t i=0; i<n; i++)
num[i] = 42;}
![Page 30: Pointers](https://reader035.fdocuments.net/reader035/viewer/2022062519/56815126550346895dbf4467/html5/thumbnails/30.jpg)
Array parameters cont. A parameter that is a pointer or array
may include the const keyword. No changes to the actual parameter or the array are possible in this case
bool is_42(const int* my_ptr);double average(const double data[],
… )
![Page 31: Pointers](https://reader035.fdocuments.net/reader035/viewer/2022062519/56815126550346895dbf4467/html5/thumbnails/31.jpg)
Pointers Reference Parameters
Sometimes a function needs to change a pointer parameter so that the pointer points to a new location.
Example:
void alloc_doubles(double*& ptr, int & n);
![Page 32: Pointers](https://reader035.fdocuments.net/reader035/viewer/2022062519/56815126550346895dbf4467/html5/thumbnails/32.jpg)
Pointer Reference Parameters
void alloc_doubles(double*& ptr, int & n);
…double *numbers;int array_size;alloc_doubles(numbers, array_size);…void alloc_doubles(double*& ptr, int & n){
cin>> n;ptr = new double[n];
}
![Page 33: Pointers](https://reader035.fdocuments.net/reader035/viewer/2022062519/56815126550346895dbf4467/html5/thumbnails/33.jpg)
Pointer Arithmetic The only legal arithmetic operators
on pointers are adding or subtracting an integer, or subtracting one pointer from another.
![Page 34: Pointers](https://reader035.fdocuments.net/reader035/viewer/2022062519/56815126550346895dbf4467/html5/thumbnails/34.jpg)
Pointer Arithmetic In C++, pointer arithmetic is
automatically done in units of the pointer's underlying base type.
Adding 1 to a pointer to an array element gives a pointer to the next element - regardless of whether we have an array of ints, an array of doubles, or an array of any other type.
![Page 35: Pointers](https://reader035.fdocuments.net/reader035/viewer/2022062519/56815126550346895dbf4467/html5/thumbnails/35.jpg)
Pointer Arithmetic
int ar[10];
ar + i is a pionter to the ith element beyond ar
&ar[i] is equivalent to ar + i
ar[i] is equivalent to *(ar + i)
![Page 36: Pointers](https://reader035.fdocuments.net/reader035/viewer/2022062519/56815126550346895dbf4467/html5/thumbnails/36.jpg)
What is the output?int main(){
int * array;array = new int[10];
*array = 33;*(array + 3) = 14;
cout<<array[0]<<array[3];
return 0;}