DSA-Class 05-Pointers
DSA-Class 05-Pointers
DYNAMIC OBJECTS
Data Structures and Algorithm
2
Topics
• Pointers
• Memory addresses
• Declaration
• Dereferencing a pointer
• Pointers to pointer
• Static vs. dynamic objects
• new and delete
3
Computer Memory
… … 100 … 1024 …
a
Variable a’s value, i.e., 100, is
int a = 100; stored at memory location 1024
4
Pointers
• A pointer is a variable used to store the
address of a memory cell.
• We can use the pointer to reference this
memory cell
… … 100 … 1024 …
integer
pointer
5
Pointer Types
• Pointer
• C++ has pointer types for each type of object
• Pointers to int objects
• Pointers to char objects
• Pointers to user-defined objects
(e.g., RationalNumber)
• Even pointers to pointers
• Pointers to pointers to int objects
6
Pointer Variable
Examples:
int *n;
RationalNumber *r;
int **p; // pointer to pointer
7
… … 100 … … …
a
int a = 100;
//get the value,
cout << a; //prints 100
//get the memory address
cout << &a; //prints 1024
8
… 88 100 … … …
a b
#include <iostream>
using namespace std;
void main(){ Result is:
The address of a is: 1020
int a, b;
The address of b is: 1024
a = 88;
b = 100;
cout << "The address of a is: " << &a << endl;
cout << "The address of b is: " << &b << endl;
}
9
Pointer Variables
Memory address: 1020 1024 1032
… 88 100 … 1024 …
a p
int a = 100; Result is:
int *p = &a; 100 1024
cout << a << " " << &a <<endl; 1024 1032
cout << p << " " << &p <<endl;
Pointer to Pointer
58 58 58
11
Dereferencing Operator *
• We can access to the value stored in the variable
pointed to by using the dereferencing operator (*),
… 88 100 … 1024 …
a p
int a = 100;
int *p = &a; Result is:
cout << a << endl; 100
cout << &a << endl; 1024
cout << p << " " << *p << endl; 1024 100
cout << &p << endl; 1032
12
A Pointer Example
Memory Layout
The code Box diagram
main
void doubleIt(int x,
int * p) p
a 16 8192
{ (8200)
*p = 2 * x; doubleIt
x 9
}
(8196)
int main(int argc,
const char * argv[]) a
doubleIt 16 main
{ (8192)
int a = 16;
doubleIt(9, &a); x 9
return 0;
} p
a gets 18
14
Reference Variables
A reference is an additional name to
an existing memory location
Pointer: Reference:
x 9 x
9
ref
ref
int x = 9;
int x=9;
int &ref = x;
int *ref;
ref = &x;
17
Reference Variables
• A reference variable serves as an alternative name for
an object
int m = 10;
int &j = m; // j is a reference variable
cout << “value of m = “ << m << endl;
//print 10
j = 18;
cout << “value of m = “ << m << endl;
// print 18
18
Reference Variables
• A reference variable always refers to the
same object. Assigning a reference variable
with a new value actually changes the value of
the referred object.
• Reference variables are commonly used for
parameter passing to a function
19
Pass by Reference
void IndirectSwap(char& y, char& z) {
char temp = y;
y = z;
z = temp;
}
int main() {
char a = 'y';
char b = 'n';
IndirectSwap(a, b);
cout << a << b << endl;
return 0;
}
21
1000
1004
1008
1012
1016
22
Result:
Address of a[0]: 0x0065FDE4
Name as pointer: 0x0065FDE4
23
This element is
called a[0] or
*a
#include <iostream>
a[0] 2 using namespace std;
a void main(){
a[1] 4 int a[5] = {2,4,6,8,22};
a[2] 6 cout << *a << " "
<< a[0];
a[3] 8 } //main
a[4] 22
a
24
#include <iostream>
p using namespace std;
a
void main(){
a[0] 2 int a[5] = {2,4,6,8,22}; 22
a[1] 4 int *p = a;
a[2] 6 cout << a[0] << " "
a[3] 8 << *p;
a[4] 22 }
a
25
#include <iostream>
a[0] using namespace std;
a[0] 22 void main(){
2 p
44 int a[5] = {2,4,6,8,22};
a[1] 4 int *p = &a[1];
a[2] 6 cout << a[0] << " "
<< p[-1];
a[3] 8 p[0]
cout << a[1] << " "
a[4] 22 << p[0];
}
26
Pointer Arithmetic
Givena pointer p, p+n refers to the element
that is offset from p by n positions.
a 2 p - 1
a + 1 4 p
a + 2 6 p + 1
a + 3 8 p + 2
a + 4 22 p + 3
27
a[0] or *(a + 0) 2 a
a[1] or *(a + 1) 4 a + 1
a[2] or *(a + 2) 6 a + 2
a[3] or *(a + 3) 8 a + 3
a[4] or *(a + 4) 22 a + 4
NULL pointer
• NULL is a special value that indicates an empty pointer
• If you try to access a NULL pointer, you will get an error
int *p;
p = 0;
cout << p << endl; //prints 0
cout << &p << endl;//prints address of p
cout << *p << endl;//Error!
30
int oned[12];
for(int i=0; i<3; i++){
for(int j=0; j<4 ; j++)
oned[i*4+j] = twod[i][j];
}
31
table[ 2] or *( table + 2 )
int table[3][4] = {{1,2,3,4}, *(table[i]+j)
What is = table[i][j]
{5,6,7,8},{9,10,11,12}};
**table
? for(int i=0; i<3; i++){
for(int j=0; j<4; j++)
cout << *(*(table+i)+j);
cout << endl;
}
Dynamic Objects
33
Memory Management
• Static Memory Allocation
• Memory is allocated at compilation time
• Dynamic Memory
• Memory is allocated at running time
34
• Object memory is
returned by a deallocation
request
• delete operation
35
Memory Allocation
new
delete
{ int* ptr;
int a[200]; ptr = new int[200];
… …
} delete [] ptr;
36
Example
int* p = new int;
p
37
p 10
38
• Syntax
P = new SomeType[Expression];
• Where
• P is a pointer of type SomeType
• Expression is the number of objects to be
constructed -- we are making an array
Example
Dynamic Memory Allocation
Request for “unnamed” memory from the Operating System
new
int *p, n=10;
p = new int;
p
new
p = new int[100]; p
new
p = new int[n]; p
40
. . .
printMean( grades, n ); // call a function with dynamic array
. . .
}
41
Initialize
}
44
print()
Adding Elements
// for adding a new element to end of array
int* addElement(int list[], int& size, int value){
int* newList = new int [size+1]; // make new array
if(newList==0){
cout << "Memory allocation error for addElement!" << endl;
exit(-1);
}
for(int i=0; i<size; i++)
newList[i] = list[i];
if(size) delete [] list;
newList[size] = value;
size++;
return newList;
}
46
int * A = NULL;
int size = 0;
int i;
delete [] A;
Locations do not belong to program
B[0] = 1; // illegal!
A —
?
B
51
A 0 1 2 3 4
— — — — —
52
A Dynamic 2D Array
A dynamic array is an table 32 18 12 24
array of pointers to
table[0]
save space when not all 13 11 16 12 42 19 14
rows of the array are table[1]
full. table[2]
22
table[3]
int **table; table[4] 13 13 14
table[5]
11 18
table = new int*[6];
…
table[0] = new int[4];
table[1] = new int[7];
table[2] = new int[1];
table[3] = new int[3];
table[4] = new int[2];
table[5] = NULL;
53
Memory Allocation
int **table;
Memory Deallocation
• Memory leak is a serious bug!
• Each row must be deleted individually
• Be careful to delete each row before deleting
the table pointer.
• for(int i=0; i<6; i++)
delete [ ] table[i];
delete [ ] table;
55
int m, n; int m, n;
cin >> m >> n >> endl;
cin >> m >> n >> endl;
int** mat;
mat = imatrix(m,n);
int** mat; …