2 Pointers
2 Pointers
(COMP-112)
ch
311 65
Introduction to Pointers
• How to get the memory-address of a variable?
• Example:
int* P;
Output:
42
42
Pointer Assignment and
Dereferencing
• Assignment operator ( = ) is used to assign value of one
pointer to another
p = new int; 0
*p = 99; text
data
return 0;
} bss
heap
0xffffffff stack
Example
int main()
{
int *p;
p = new int; 0
*p = 99; text
data
return 0;
} bss
heap
#@%*&
0xffffffff stack
Example
int main()
{
int *p;
p = new int; 0
*p = 99; text
data
return 0;
} bss
heap
99
0xffffffff stack
Aliasing
int main()
{
int *p, *q;
p = new int; 0
*p = 99; text
q = p; data
return 0; bss
}
heap
99
q
p
0xffffffff stack
Aliasing
int main()
{
int *p, *q;
p = new int; 0
*p = 99; text
q = p; data
*q = 88; bss
return 0; heap
}
88
q
p
0xffffffff stack
Aliasing
int main()
{
int *p, *q;
p = new int; 0
*p = 99; text
q = p; data
*q = 88; bss
delete q; heap
return 0; $%#^&
}
q
p
0xffffffff stack
Dangling Pointers
int main()
{
int *p, *q;
p = new int; 0
*p = 99; text
q = p;
P and q are dangling pointers data
*q = 88; WHY?
bss
delete q;
heap
*p = 77;
$%#^&
return 0;
}
q
p
0xffffffff stack
Dangling Pointers
• The delete operator does not delete the pointer, it
takes the memory being pointed to and returns it to
the heap
• For Variables:
delete v1;
v1 = NULL;
• For Arrays:
delete[ ] arr;
arr = NULL;
Returning Memory to the
Heap
• Remember:
• Return memory to the heap before undangling the pointer
void f ( )
{
int *p;
p = new int;
return;
}
int main ( )
{
f ( );
return 0;
}
Memory Leaks
• Memory leaks when it is allocated from the heap using the new
operator but not returned to the heap using the delete operator
Memory Leaking and Dangling
Pointers
• Dangling pointers and memory leaking are evil sources of
bugs:
• hard to debug
• may appear after a long time of run
• may be far from the bug point
• hard to prevent
void main()
{
int numbers[]={10,20,30,40,50};
cout<<numbers[0]<<endl; 10
cout<<numbers<<endl; Address e.g., &34234
cout<<*numbers<<endl; 10
cout<<*(numbers+1); 20
}
Arrays and Pointers
Array name is the starting address of the array
• Let p = A;
int iVar=5;
This is a great advantage…
float fVar=4.3; So, What are the limitations/cha
char cVar=‘Z’;
int* p1;
void* vp2;
p1 = &iVar; // Allowed
p1 = &fvar; // Not Allowed
P1 = &cVar; // Not Allowed
vp2 = &fvar; // Allowed
vp2 = &cVar; // Allowed
vp2 = &iVar; // Allowed
Accessing 1-Demensional Array
Using Pointers
• We know, Array name denotes the memory address of
its first slot. Address Data
• Example: 980 Element 0
int List [ 50 ]; 982 Element 1
int *Pointer; 984 Element 2
Pointer = List; 986 Element 3
988 Element 4
• Other slots of the Array (List [50]) can be accessed by
performing Arithmetic operations on Pointer. 990 Element 5
992 Element 6
• For example the address of (element 4 ) can be
th
994 Element 7
accessed using:-
996 Element 8
int *Value = Pointer + 3;
• The value of (element 4th) can be accessed using:- …
int Value = *(Pointer + 3); …
998 Element 49
Accessing 1-Demensional Array
Address Data
…. 980 Element 0
…. 982 Element 1
int List [ 50 ]; 984 Element 2
int *Pointer; 986 Element 3
293
Pointer = List; // Address of first Element 988 Element 4
990 Element 5
int *ptr; 992 Element 6
ptr = Pointer + 3; // Address of 4th 994 Element 7
Element 996 Element 8
*ptr = 293; // 293 value store at 4th
…
element address
…
} 998 Element 49
Accessing 1-Demensional Array
We can access all element of List [50] using
Pointers and for loop combinations. Address Data
980 Element 0
… 982 Element 1
… 984 Element 2
{ 994 Element 7
…
Represents that we are accessing the address …
of 4th row 998 Element 50
Row
column: 4 348 350 352 354 356 358
5 360 362 364 366 368 370
• ptr++; // address of 4th row 2nd
6 372 374 376 378 380 382
column 7 384 386 388 390 392 394
• (faster than normal array accessing 8 396 398 400 402 404 406
Why?)
• Equivalent to List [3][1] ;
Memory address
Differences between Static and Dynamic
Memory Allocation
• It is possible to run out of memory (you may even have gotten a message like
"Running Low On Virtual Memory")
• So, it is important to return memory to the heap when you no longer need it
Casting pointers
Pointers have types, so you cannot just do
5 rows * 4 columns
= 20 elements
Target Approach=
• allocate 20 elements using dynamic allocation
• Use a single pointer to point and access those items.
Dynamic 2D Arrays
Dynamic 2D Array – Double Pointer
2. Using a Pointer that points to Array of Pointer
• Total elements in a 2D Array: M_rows * N_coulmns
Ptr2D
(Pointer to a Pointer)
Dynamic 2D Array – Double Pointer
Dynamic two
dimensional
arrays
char c = 'c';
char d = 'd';
char* const ptr1 = &c;
ptr1 = &d; // Not Allowed
int var1 = 0;
const int* ptr = &var1;
*ptr = 1; // Not Allowed
cout<<*ptr;
C-String and Char
Pointer
• A String: is simply defined as an array of characters
char* s;
// s is the address of the first character (byte) of the string
char* s=“PAF-IAST”;
cout<<s;
cout<<++s;
C-String and Char Pointer
C-String and Char Pointer - Example
void main()
{
int n = 5;
cout<<"Before call: n = "<<n<<endl;
func(&n);
cout<<"After call: n = "<<n<<endl;
}
Pass by Reference Pointers– Example2
c
void compDouble(int* Ar)
{
for(int i=0;i<10;i++)
{ *Ar=(*Ar)*2;
Ar++;
}
}
void main()
{ int Arr[10]={0,1,2,3,4,5,6,7,8,9};
compDouble(Arr);
for(int i=0;i<10;i++)
cout<<Arr[i]<<endl;
}
Pass by Reference Pointers– Example2
c
void compDouble(int* Ar)
{
for(int i=0;i<10;i++)
{ *Ar=(*Ar)*2;
Ar++;
}
}
void main()
{ int Arr[10]={0,1,2,3,4,5,6,7,8,9};
compDouble(Arr);
for(int i=0;i<10;i++)
cout<<Arr[i]<<endl;
}
Questions (last lecture)
c
Address of character variable…