CSC2110 - Data Structures/Algorithms
CSC2110 - Data Structures/Algorithms
CSC2110 - Data Structures/Algorithms
Pointers
Pointers
Powerful feature of the C++ language
One of the most difficult to master
Essential for construction of interesting data
structures
variables
Name (C++ keeps track of the address of the
that follows it
Addresses/Pointers can be displayed by the
cout statement
Addresses displayed in HEXADECIMAL
CSC2110 - Data Structures/Algorithms
Example
#include <iostream.h>
void main( )
{
int data = 100;
float value = 56.47;
cout << data << &data << endl;
cout << value << &value << endl;
}
valu
e
FFF0
56.47
FFF1
FFF2
FFF3
data
FFF4
100
FFF5
FFF6
Output:
100 FFF4
56.47 FFF0
CSC2110 - Data Structures/Algorithms
Pointer Variables
The pointer data type
A data type for containing an address rather
than a data value
Integral, similar to int
Size is the number of bytes in which the target
computer stores a memory address
Provides indirect access to values
Declaration of Pointer
Variables
A pointer variable is declared by:
dataType *pointerVarName;
The pointer variable pointerVarName is used to
point to a value of type dataType
The * before the pointerVarName indicates that
this is a pointer variable, not a regular variable
The * is not a part of the pointer variable name
Declaration of Pointer
Variables
(Cont
..)
Example
int *ptr1;
float *ptr2;
ptr1 is a pointer to an int value i.e., it can
have the address of the memory location (or
the first of more than one memory locations)
allocated to an int value
ptr2 is a pointer to a float value i.e., it can
have the address of the memory location (or
the first of more than one memory locations)
allocated to a float value
CSC2110 - Data Structures/Algorithms
Declaration of Pointer
Variables
(Cont
..)
Assignment of Pointer
Variables
A pointer variable has to be assigned a
Assignment of Pointer
Variables (Cont ..)
FFF0
FFF1
FFF2
float *ptr;
FFF3
ptr = &data;
data
FFF4
50.8
FFF5
FFF6
10
Assignment of Pointer
Variables (Cont ..)
ptr
FFF0
FFF1
FFF2
float *ptr;
FFF3
ptr = &data;
data
FFF4
50.8
FFF5
FFF6
11
Assignment of Pointer
Variables (Cont ..)
ptr
FFF0
FFF4
FFF1
FFF2
float *ptr;
FFF3
ptr = &data;
data
FFF4
50.8
FFF5
FFF6
12
Assignment of Pointer
Variables
(Cont ..)
Dont try to assign a specific integer value to a
pointer variable since it can be disastrous
float *ptr;
ptr = 120;
13
Initializing pointers
A pointer can be initialized during declaration
14
data type.
value is NULL.
Such an error may cause your program to
15
Dereferencing
Dereferencing Using a pointer variable to
called indirection
16
17
used in assignments.
*ptr = 200;
Make sure that ptr has been properly initialized
18
Dereferencing Example
#include <iostream.h>
void main()
{
float data = 50.8;
float *ptr;
ptr = &data;
cout << ptr << *ptr << endl;
*ptr = 27.4;
cout << *ptr << endl;
cout << data << endl;
}
ptr
FFF0
FFF4
FFF1
FFF2
FFF3
data
FFF4
50.8
FFF5
FFF6
Output:
CSC2110 - Data Structures/Algorithms
19
#include <iostream.h>
void main()
{
float data = 50.8;
float *ptr;
ptr = &data;
cout << ptr << *ptr << endl;
*ptr = 27.4;
cout << *ptr << endl;
cout << data << endl;
}
ptr
FFF0
FFF4
FFF1
FFF2
FFF3
data
FFF4
50.8
FFF5
FFF6
Output:
FFF4 50.80
20
Dereferencing Example
(Cont ..)
#include <iostream.h>
void main()
{
float data = 50.8;
float *ptr;
ptr = &data;
cout << ptr << *ptr << endl;
*ptr = 27.4;
cout << *ptr << endl;
cout << data << endl;
}
ptr
FFF0
FFF4
FFF1
FFF2
FFF3
data
FFF4
27.4
FFF5
FFF6
Output:
CSC2110 - Data Structures/Algorithms
21
Dereferencing Example
(Cont ..)
#include <iostream.h>
void main()
{
float data = 50.8;
float *ptr;
ptr = &data;
cout << ptr << *ptr << endl;
*ptr = 27.4;
cout << *ptr << endl;
cout << data << endl;
}
ptr
FFF0
FFF4
FFF1
FFF2
FFF3
data
FFF4
27.4
FFF5
FFF6
Output:
27.4
22
Dereferencing Example
(Cont ..)
#include <iostream.h>
void main()
{
float data = 50.8;
float *ptr;
ptr = &data;
cout << ptr << *ptr << endl;
*ptr = 27.4;
cout << *ptr << endl;
cout << data << endl;
}
ptr
FFF0
FFF4
FFF1
FFF2
FFF3
data
FFF4
27.4
FFF5
FFF6
Output:
27.4
23
Operations on Pointer
Variables
Assignment the value of one pointer
24
Pointers to arrays
A pointer variable can be used to access
25
Dynamic Memory
Allocation
26
27
Allocation of Memory
Static Allocation: Allocation of memory space
at compile time.
Dynamic Allocation: Allocation of memory
28
Dynamic memory
allocation
Dynamic allocation is useful when
arrays need to be created whose extent is not
known until run time
complex structures of unknown size and/or
shape need to be constructed as the program
runs
objects need to be created and the constructor
arguments are not known until run time
29
Dynamic memory
allocation
Pointers need to be used for dynamic
allocation of memory
Use the operator new to dynamically allocate
space
Use the operator delete to later free this
space
30
31
effect.
32
Example
int *ptr;
ptr
FDE0
FDE1
FDE2
FDE3
ptr = NULL;
0EC4
0EC5
0EC6
0EC7
CSC2110 - Data Structures/Algorithms
33
ptr
FDE0
0EC4
FDE1
FDE2
FDE3
ptr = NULL;
0EC4
0EC5
0EC6
0EC7
CSC2110 - Data Structures/Algorithms
34
ptr
FDE0
0EC4
FDE1
FDE2
FDE3
ptr = NULL;
0EC4
22
0EC5
0EC6
0EC7
CSC2110 - Data Structures/Algorithms
35
ptr
FDE0
0EC4
FDE1
FDE2
FDE3
ptr = NULL;
0EC4
Output:
22
0EC5
22
0EC6
0EC7
CSC2110 - Data Structures/Algorithms
36
ptr
FDE0
FDE1
FDE2
FDE3
ptr = NULL;
0EC4
0EC5
0EC6
0EC7
CSC2110 - Data Structures/Algorithms
37
ptr
FDE0
FDE1
FDE2
FDE3
ptr = NULL;
0EC4
0EC5
0EC6
0EC7
CSC2110 - Data Structures/Algorithms
38
39
40
Dynamic allocation of 2D
arrays
A two dimensional array is really an array of
arrays (rows).
To dynamically declare a two dimensional
array of int type, you need to declare a
pointer to a pointer as:
int **matrix;
41
Dynamic allocation of 2D
arrays
(Cont
..)
To allocate space for the 2D array with r
rows and c columns:
a pointer to an int.
42
Dynamic allocation of 2D
arrays (Cont ..)
43
Example
// create a 2D array dynamically
int rows, columns, i, j;
int **matrix;
cin >> rows >> columns;
matrix = new int*[rows];
for(i=0; i<rows; i++)
matrix[i] = new int[columns];
// deallocate the array
for(i=0; i<rows; i++)
delete [] matrix[i];
delete [] matrix;
CSC2110 - Data Structures/Algorithms
44
Passing pointers to a
function
45
Pointers as arguments
to functions
Pointers can be passed to functions just
46
Example of pointer
arguments
void Swap(int *p1, int *p2);
void main ()
{
int x, y;
cin >> x >> y;
cout << x << " " << y << endl;
Swap(&x,&y); // passes addresses of x and y explicitly
cout << x << " " << y << endl;
}
void Swap(int *p1, int *p2)
{
int temp = *p1;
*p1 = *p2;
*p2 = temp;
}
47
Example of reference
arguments
void Swap(int &a, int &b);
void main ()
{
int x, y;
cin >> x >> y;
cout << x << " " << y << endl;
Swap(x,y); // passes addresses of x and y implicitly
cout << x << " " << y << endl;
}
void Swap(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
}
CSC2110 - Data Structures/Algorithms
48
More example
void main ()
{
int r, s = 5, t = 6;
int *tp = &t;
r = MyFunction(tp,s);
r = MyFunction(&t,s);
r = MyFunction(&s,*tp);
}
int MyFunction(int *p, int i)
{
*p = 3;
i = 4;
return i;
}
CSC2110 - Data Structures/Algorithms
49
50
Memory leaks
When you dynamically create objects, you
51
ptr1
8
ptr2
5
ptr1
8
How to avoid?
ptr2
5
52
Inaccessible object
An inaccessible object is an unnamed object
53
Dangling Pointer
It is a pointer that points to dynamic
54
Dangling Pointer
example
int *ptr1 = new int;
int *ptr2;
*ptr1 = 8;
ptr2 = ptr1;
delete ptr1;
ptr1
8
ptr2
ptr1
How to avoid?
ptr2
55
Pointers to objects
Pointers to objects
Any type that can be used to declare a
57
rp
FFF0
FFF1
FFF2
FFF3
FFF4
FFF5
FFF6
FFF7
FFF8
FFF9
FFFA
FFFB
FFFC
FFFD
58
rp
numerator = 3
denominator =
4
FFF0
FFF1
FFF2
FFF3
FFF4
FFF5
FFF6
FFF7
FFF8
FFF9
FFFA
FFFB
FFFC
FFFD
59
rp
numerator = 3
denominator =
4
FFF0
FFF1
FFF2
FFF3
FFF4
FFF5
FFF6
FFF7
FFF8
FFF9
FFFA
FFFB
FFFC
FFFD
FFF4
60
Pointers to objects
(Cont..)
If rp is a pointer to an object, then two
61
62