Pointers
C++ Programming
What is Pointer ?
Pointer points to a memory
Car pointer 10
2001
Pointer knows how to reach to that memory
location.
pointer
2001 10
2001
Why Pointers ?
Heap Heap var
Stack Stack
var ptr
Code Code Void main()
Void main() {
{ Int var[4] = {1,2,3,4};
Int no = 10;; Int *ptr = var;
Cout << no; Cout << (*ptr);
} }
Why Pointers ?
Pointer Uses
Use Properly
1. Directly Accessing Memory
2. Accessing Array Elements
3. Passing Arrays and strings to function
4. For creating Data Structures like
linked list
Pointer Usage
virtual functions --- new Operator ----- this pointer
Program in Memory
Memory
var1 20001 FLOAT
20002
20003
program
20004
var2 20005 INT
20006
Pointers Notation
void main ( )
{
Data_Type *POINTER_NAME; int number = 10;
int *ptr = NULL;
int *ptr = NULL; ptr = &number;
cout << ptr; //pointer address
number ptr
cout << ( *ptr ); // value pointed
500
10 2090
by pointer
2090 79349 }
Pointer And Arrays
void main ( )
{
int arr[5]= { 10, 20, 30, 40, 50 };
0 1 2 3 4
for ( int i =0; i<5; i++ )
{ 10 20 30 40 50
cout << arr[i] << endl;
}
}
Pointer And Arrays
void main ( ) ptr Memory
{ 20001 10 20001
int arr[5]= { 10, 20, 30, 40, 50 }; 20002
79349
int *ptr = arr; 20 20003
20004
for ( int i =0; i<5; i++ ) 30 20005
{ 20006
cout << *( ptr + i ) << endl; 40 20007
}
20008
}
50 20009
20010
ptr (20001)
Pointers And Function
void main( ) temp
{ void square( int *ptr )
int number = 10; { 10
int temp = *ptr; 40001
cout << number;
temp = temp*temp; temp
square ( &number );
100
10
*ptr = temp;
cout << number; } 40001
}
number number ptr number ptr
10 10 20001 100 20001
20001 20001 20001
Pointers And Function (Passing Array)
#include<iostream> void printArray( int *ptr )
using namespace std; {
const int MAX = 5; for( int i=0; i<MAX; i++ )
{
void main( ) cout << *ptr++;
{ }
int number[MAX] = {10,20,30,40,50}; }
0 1 2 3 4
printArray ( number ); // &number[0]
10 20 30 40 50
} 110 112 114 116 118
110 112
ptr ptr
Memory Management
Static Memory Allocation Dynamic Memory Allocation
Static Memory Allocation, is used when we Dynamic Memory Allocation, is used when we
know amount of memory needed ( like we have don't know amount of memory needed ( like no
upper limit. upper limit.
Static Memory Allocation, is done automatically Dynamic Memory Allocation, is done manually
by your compiler. by programmer.
Static Memory Allocation, is allocated on STACK. Dynamic Memory Allocation, is allocated on
HEAP.
new - allocation / delete - deallocation
Memory Management: new
> Syntax to use new operator:
ptr
int *ptr = NULL; 0 pointer is initialized with NULL
ptr
ptr = new int; 2001 Request for memory
2001
or
int *ptr = new int;
> Syntax to Initialize memory:
ptr
int *ptr = new int(10); 2001 10 Initialize memory using new
2001
Memory Management: new
> Syntax to Allocate block of memory:
ptr
int *ptr = new int [5]; 2001
2001 2003 2005 2007 2009
*ptr = 10;
ptr
*( ptr + 1 ) = 20; 2001 10 20 30 40 50
2001 2003 2005 2007 2009
*( ptr + 2 ) = 30;
Memory Management: new
class Test
void main( )
{
{
private:
Test *t2; // pointer to Test
int data;
t2 = new Test; // points to new Test Object
public:
void setData( int set )
t2->setData(10);
{ data = set; }
cout << t2->getData( );
int getValue( )
{ return data; }
}
};
Memory Management: delete
Delete is use to deallocate dynamically allocated memory.
> Syntax to Deallocate memory pointed by pointer:
int *ptr = new int; delete ptr;
> Syntax to Deallocate block of memory pointed by pointer:
int *ptr = new int[10]; delete[] ptr;
Pointers Limitations
problem
1. Uninitialized Pointer. dataType *pointerName;
Memory
pointer
121G solution
dataType *pointerName = &variable;
dataType *pointerName = NULL;
dataType *pointerName = new dataType;
Pointers Limitations
problem ( not using )
2. Memory Leaks. delete pointerName;
Memory
Used Unused
pointer
1000 1000 .......
solution ( use )
delete pointerName;
pointer
delete [] pointerName;
2000
Pointers Limitations
3. Dangling Pointer.
pointer1
Unknow
1000
Unreserved
Reserved Memory
Memory
pointer2
pointer2
1000
1000 1000
Program Crash