CS 217 2 Pointers
CS 217 2 Pointers
(CS 217)
running
• Process process
– Managed by OS
C++ Memory Models
• C++ leaves memory management mostly up to the
programmer:
ve++: write programs that use memory very
efficiently
Ve--: write programs that waste memory or do not
work at all
Network Audio
Data Bus
CPU
Disk Video
Memory
0xffffffff
Organization of Virtual Memory: .text
0xffffffff
Organization of Virtual Memory: .data
bss
0xffffffff
Organization of Virtual Memory: heap
data
bss
heap
0xffffffff
Organization of Virtual Memory: stack
bss
– grow to low address
heap
stack
0xffffffff
Summary: Process Address Space
• text: program text/code
0
text
• data: initialized globals & static data data
bss
• bss: un-initialized globals & static data heap
stack
0xffffffff
• stack: function’s local variables
Introduction to Pointers
• When we declare a variable, some memory is
allocated for it.
– Example:
int* P;
int v1=99;
int* p= &v1;
cout<<“ P points to the value: “<<*p;
Dereferencing Pointer Example
int v1 = 0;
v1 and *p1 now refer to
int* p1 = &v1; the same variable
*p1 = 42;
cout << v1 << endl;
cout << *p1 << endl;
Output:
42
42
Pointer Assignment and Dereferencing
• Assignment operator ( = ) is used to assign value
of one pointer to another
return p;
}
stack
0xffffffff
Example
return p;
}
stack
0xffffffff
Variable Lifetime
• text:
– program startup
– program finish
0
• data, bss: text
– program startup
data
– program finish
bss
• heap:
– dynamically allocated heap
– de-allocated (free)
• stack:
– function call
– function return stack
0xffffffff
Example
return p;
}
live after allocation; till
delete or program finish stack
0xffffffff
Variable Initialization
• text:
– Read-only (once; e.g., constants)
0
text
• data
data
– on program startup
bss
• bss:
heap
– un-initialized (though some systems initialize with 0)
• heap:
– un-initialized
stack
0xffffffff
• stack:
– un-initialized
Dynamic Memory Allocation
• Used when space requirements are unknown at
compile time
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;
0
p = new int; text
*p = 99;
data
q = p;
bss
return 0;
heap
}
99
q
p
0xffffffff stack
Aliasing
int main()
{
int *p, *q;
0
p = new int; text
*p = 99;
q = p; data
bss
*q = 88;
heap
return 0;
} 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 Arrays:
delete[ ] arr;
arr = NULL;
Returning Memory to the Heap
• Remember:
– Return memory to the heap before undangling the
pointer
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
– hard to prevent
Effect:-
- List is an address, no need for &
- The bPtr pointer will contain the address of the first
element of array List.
– Element List[2] can be accessed by *( bPtr + 2 )
Relationship between Arrays and Pointers
• Arrays and pointers are closely related:
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;
float fVar=4.3;
This is a great advantage…
char cVar=‘Z’;
So, What
int* p1; are the limitations/challenges?
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
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 Element 994 Element 7
*ptr = 293; // 293 value store at 4th element 996 Element 8
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
– int *ptr; 0 1 2 3 4 5
• To access the address of 4th row 2nd 3 336 338 340 342 344 346
Row
4 348 350 352 354 356 358
column: 5 360 362 364 366 368 370
– ptr++; // address of 4th row 2nd 6 372 374 376 378 380 382
7 384 386 388 390 392 394
column 8 396 398 400 402 404 406
– (faster than normal array accessing
Why?)
– Equivalent to List [3][1] ; Memory address
Accessing 2-Demensional Array
• We know computer can perform only
one operation at any time (remember Column
fetch-decode-execute cycle). 0 1 2 3 4 5
Row
– First to determine row List [3] 4 348 350 352 354 356 358
– Second to determine column List[3][1] 5 360 362 364 366 368 370
6 372 374 376 378 380 382
7 384 386 388 390 392 394
• But using pointer we can reach the 8 396 398 400 402 404 406
element of 4th row 2nd column (directly)
by increment our pointer value (which is
a single operation).
– ptr+1; // 4th row 2nd column Memory address
– ptr+2; // 4th row 3rd column
– ptr+3; // 4th row 4th column
Differences between Static and Dynamic Memory
Allocation
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=“FAST”;
cout<<s<<sizeof(s);
cout<<++s<<sizeof(s);
char [ ] VS. char *
char A[20]=“FAST”; char* P=“FAST”;
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…