pointers (pp tminimizer)
DESCRIPTION
TRANSCRIPT
POINTERS IN C++
POINTERS : Introduction• Every byte in computer memory has an address.
• Addresses are numbers just like house numbers.
• Addresses start from 0 and go up as 1, 2, 3….e.g. in 640 KB memory addresses will be 0 to 655359 I.e., (640 X 1024)-1
• The moment a variable is declared, memory is allocated in the RAM for its storage. int X=5;
• A variable in the RAM can be accessed only if you know its address
POINTERS : Introduction
• A pointer in c++ gives the address of a variable in RAM
• It gives direct access to the RAM .
• A variable storing a memory address is called a Pointer
04/08/23 Prepared by Nita Arora 4
Uses of Pointers• Pointers can be used for swapping variables without
changing their physical address in memory.
• A pointer helps in traversing an array.
• Pointers are used to pass arguments to functions by reference.
• Pointers are used to allocate memory dynamically and deallocate it.
• Pointers are used for creation of data structures like linked lists.
04/08/23 Prepared by Nita Arora 5
DECLARATION AND INITIALIZATION OF POINTERS
• Pointer variables are declared like normal variables except for addition of unary * character as shown:
type * var_name;
e.g., int * iptr;
float * fptr;
char * cptr;
04/08/23 Prepared by Nita Arora 6
TYPES OF POINTERS• Pointer to Integer
int * ptr;
• Pointer to character
char * z;
Type of variable pointed to by ptr
Reference operator
Name of the pointer
Type of variable pointed to by z
Name of the pointer
Reference operator
04/08/23 Prepared by Nita Arora 7
• Pointer to floatconsider the following program segment:
float marks=76.50;
float *p;Currently p is not pointing to any variable
76.50
marks
A variable of type floatIf following statement is added in the above program segment:p = & marks;
The variable p will point to marks of the type float as shown below:
76.50
marksp
A variable of type floatNow pointer p ispointing to marks
p
04/08/23 Prepared by Nita Arora 8
•Pointer to Void• This is general purpose pointer that can point
to any data type.• It is defined as
void * ptr ;• Limitation: The data pointed to cannot be
referenced directly. (We cannot use * operator on them.), since their length is undetermined. We will have to resort to type casting.
04/08/23 Prepared by Nita Arora 9
Null Pointer
• A pointer variable must not remain un-initialized.
• If you do not have any legal value, you can initialize it with NULL pointer value as shown below:
float * ptr=NULL;
04/08/23 Prepared by Nita Arora 10
Two special operators used with Pointers are :
• Asterisk (*) and
• Ampersand (&)
04/08/23 Prepared by Nita Arora 11
THE ‘ADDRESS OF’ OPERATOR (&)Consider following: int x=10; int * ptr;
0 1 2 3 4 . . . . .
In C++ & is address of operator.If x is a variable with value 10 &x is its address i.e., 1000 as
shown above.It is a valid statement : ptr=&x; ptr will get 1000 i.e., the
address of variable x.
n15
Address of x
memory
Two bytes required for storing one integer
04/08/23 Prepared by Nita Arora 12
VALUE AT OPERATOR ( * )
• The unary operator (*) returns the value of the variable located at address following it.
• e.g.,if ptr=&x; i.e., ptr contains the address of x then giving the command :
cout << * ptr;
will display the value held by the address 1000 which is 15.
NOTE : The operand of & operator is any variable but operand of * operator is essentially a pointer variable.
04/08/23 Prepared by Nita Arora 13
MEMORY ALLOCATION• Pointers provide an essential tool for
increasing the power of C++ by handling memory.
• When C++ program is compiled, computer memory is divided into four regions as shown:
STACK
HEAP
GLOBALVAR.
PROG.CODE
Area used for function calls, addresses, arguments and local variables
Area used for dynamic allocation of memory
04/08/23 Prepared by Nita Arora 14
Static And Dynamic Memory Allocation
• Static Memory Allocation : When memory is allocated at the time of compilation.e.g.,
int a[50];
• Dynamic Memory Allocation: Memory is allocated during run time using new operator from the heap memory area. The delete operator de-allocates the memory.
04/08/23 Prepared by Nita Arora 15
Memory Management : Operators new and delete
• Arrays are used to set aside memory. The statement
int a[50];
reserves memory for 50 integers. • The following approach doesn’t work:
cin>>size;
int a[size];//error array size must be constant
04/08/23 Prepared by Nita Arora 16
The new Operator
• new operator requests for memory allocation dynamically.
• It returns a pointer to the beginning of the block of assigned memory. Its form is:
pointer = new type;
or
pointer = new type[Number of elements];
04/08/23 Prepared by Nita Arora 17
For example:
int *ptr;
ptr = new int[5];
• In this example, the operating system assigns space for 5 elements of type int in the HEAP and returns address to the variable ptr.
n Heapmemory
ptr
Memory space for 5 int
04/08/23 Prepared by Nita Arora 18
Free Storage Pool
• Every program is given some unallocated heap memory that is used by the program for dynamic memory allocation during execution. This is known as free storage pool.
04/08/23 Prepared by Nita Arora 19
The delete Operator
• Dynamic memory when not required should be freed so that it becomes available for future use.
• Operator delete can be used for this purpose, whose form is :
delete ptr;
or
delete [ ] ptr;
04/08/23 Prepared by Nita Arora 20
• If the program reserves many chunks of memory using new, eventually all the variable memory will be reserved and the system will crash.
• To ensure safe and efficient use of memory, the use of new operator is followed by corresponding delete operator that returns memory to operating system.
The delete Operator (Contd.)
04/08/23 Prepared by Nita Arora 21
• If a function allocates memory dynamically but forgets to release it later, it occupies some amount of memory every time it is executed.
• It will leave abandoned memory blocks. This situation is known as a memory leak.
Memory Leaks
Reasons for Memory Leak
• Forgetting to use delete operator.• The delete operator is in an unreachable code area
of the program.• Using new operator for allocating memory with a
pointer that is already pointing to an allocated object.
04/08/23 Prepared by Nita Arora 22
Give the output of the following:Pointer prg1.cpp
#include <iostream.h>#include <conio.h>void main(){ clrscr(); int * ptr; int i=25; ptr=&i; cout<<i<<‘\n’; cout<<&i<<‘\n’; cout<<ptr<<‘\n’; cout<<* ptr <<‘\n’; cout<< <*<*(& i)<<‘\n’;}
04/08/23 Prepared by Nita Arora 23
Give the output of the following:Pointer prg2.cpp
#include <iostream.h>#include <conio.h>void main(){ clrscr(); char * cp; char ch=‘a’; cp=& ch;
cout<<* ptr <<‘\n’; cout<< <*<*(& i)<<‘\n’;}
04/08/23 Prepared by Nita Arora 24
Give the output of the following:Pointer prg3.cpp
#include <iostream.h>#include <conio.h>void main(){ clrscr(); char *p1,*p2,*p3; int x=5; int y=7; p1=&x; p2=&y; cout << “\noriginal values are : “;<<*p1<<*p2; p3=p1; p1=p2; p2=p3; cout << “\nexchanged values are : “;<<*p1<<*p2; getch(); }
04/08/23 Prepared by Nita Arora 26
So, if we write:Assume following pointers:
int *iptr;////assume it points to location 1000 char *cptr;//assume it points to location 2000 float *fptr;//assume it points to location 3000
iptr++; //iptr will be incremented by 2cptr++; //cptr will be incremented by 1fptr++; //fptr will be incremented by 4
04/08/23 Prepared by Nita Arora 27
n Heapmemory
iptr10
00
1002
int *iptr;////assume it points to location 1000
n Heapmemory
iptr
1000 1002
iptr++; //iptr will be incremented by 2