Vous êtes sur la page 1sur 36

Pointers

Pointers
A pointer is a variable which holds the memory
address.
Any variable declared in a program has two
components:
1. Address of the variable
2. Value stored in the variable.
Example
Int x=386;
The above declaration tells the C++ compiler for:
1. Reservation of space in memory for storing
the value.
2. Associating the name x with this memory
location.
3. Storing the value 386 at this location.
Pictorial Representation
Location Name-> x
Value at location-> 386

Location Number-> 3313


Declaration And Initialization Of
Pointers
Declaration Of Pointer
Syntax:
Data-type * pointer-variable;
Here Pointer variable is the name of pointer, and
the data type refers to data types like int, char,
float etc.
Example:
Int *ptr;
Ptr is a pointer variable that points to integer data
type.
Initialization Of Pointers:
Ptr=&a;
The pointer variable ptr, contains the address of
the variable a.

We can also declare a pointer variable to point


to another pointer. That is pointer variable
contains address of another pointer. This is
also known as pointer to pointer.
#include<iostream.h>
#include<conio.h>
void main()
{
int a, *ptr1,**ptr2;
clrscr();
ptr1=&a;
ptr2=&ptr1;
cout<<"The address of a :" <<ptr1;
cout<<"\n";
cout<<"Address of ptr1:"<<ptr2;
cout<<"\n";
cout<<"After incrementing the address values:";
ptr1=ptr1+2;
cout<<"The address of a:"<<ptr1;
cout<<"\n";
ptr2=ptr2+2;
cout<<"The address of ptr1:"<<ptr2;
getch();
}
Manipulation of Pointers
For accessing the value of variable through a
pointer we use the indirection operator *.
With this operator, we can indirectly access the
data variable content.
This operator allows us to get the content of
memory location that the pointer points to.
#include<iostream.h>
#include<conio.h>
void main()
{
int x=386, *ptr;
ptr=&x;
clrscr();
cout<<"x=" <<x<<endl;
cout<<"x="<<*ptr; //prints the value of x through a pointer
*ptr=*ptr+10; //increments the value of x through pointer
cout<<"x="<<x;
getch();
}
Pointer Expression and Pointer
Arithmetic
A pointer can be incremented(++) or
decremented(--).
Any integer can be added to or subtracted
from pointer.
One pointer can be subtracted from other.
Pointer And Arrays
We know that the name of an array holds the
address of the first element in that array.
For Example
Char name[20];
Here name holds the address of name[0]. A
pointer also holds the address of a variable.
So we can make a conclusion that the name of
an array is actually a pointer.
#include<iostream.h>
#include<conio.h>
void main()
{
char arr[26]="Welcome to world of peace";
char *cptr;
clrscr();
cout<<"The message to all is "<<endl;
cptr=arr;
cout<<cptr;
getch();
}
Array
#include<iostream.h>
Of Pointers
#include<conio.h>
void main()
{
clrscr();
int *num[3];
int a=50,b=100,c=150;
num[0]=&a;
num[1]=&b;
num[2]=&c;
for(int i=0;i<3;i++)
{
cout<<"The pointer num["<<i<<"]points to value"<<*num[i]<<endl;
cout<<"The address of elements are"<<num[i];
}
getch();
}
Pointers To Functions
Pointers To Functions
The pointer to function is known as callback
function. We can use these function pointer to
refer to a function. Using function pointers,
we can allow a C++ program to select a
function dynamically at run time. We can also
pass a function as an argument to another
function.
Syntax
Data_type (* function_name)();

Example:
Int (*num-function(int x));
#include<iostream.h>
#include<conio.h>
int * largest(int &, int &, int &);
main()
{
int x,y,z,*max;
cout<<"Enter the three integers";
cin>>x>>y>>z;
max=largest(x,y,z);
cout<<"The largest number is "<<*max;
getch();
}
int * largest(int &a, int &b, int &c)
{
if((a>b) &&(a>c))
return &a;
else if ((b>a) && (b>c))
return &b;
else
return &c;
}
Pointers To Objects
C++ allows us to have pointers to objects known
as object pointers.

Syntax:
Class name * object_pointer;
Here class name is the name of the earlier
defined class and object_pointer is an object
pointer of type class. An -> operator is used
with an object pointer for accessing class
members.
#include<iostream.h>
#include<conio.h>
class add
{
private:
int a,b,c;
public:
void input()
{
cout<<"Enter the value of a";
cin>>a;
cout<<"Enter the value of b";
cin>>b;
}
void sum()
{
c=a+b;
cout<<"Sum of numbers is"<<c;
}
};
void main()
{
clrscr();
add *ptr;
add o1;
ptr=&o1;
ptr->input();
ptr->sum();
getch();
}
This Pointer
The this is a pointer that points to that object
using which the function is called. The this
pointer is automatically passed to a member
function when it is called.
We know that while defining a class the memory
is allocated for member function only once
and separate memory is allocated for each
object.
There exist a serious problem that is which
objects data member is to be manipulated by
any member function. For example, if
memberfunc2() is responsible for modifying
the value of datamember1 and we are
interested in modifying the value of
datamember1 of object3. In this situation
how to decide the manipulation of which
objects data member1??
#include<iostream.h>
#include<conio.h>
#include<string.h>
class per
{
char name[20];
float salary;
public:
per(char *s,float a)
{
strcpy(name,s);
salary=a;
}
per *GR(per &x)
{
if(x.salary>=salary)
{
return &x;
}
else
{
return this;
}
}
void display()
{
cout<<"Name:"<<name;
cout<<"Salary:"<<salary;
}
};
void main()
{
per p1("Reema",10000),p2("Krishan",20000),p3("George",5000);
per *p;
p=p1.GR(p3);
p->display();
p=p2.GR(p3);
p->display();
getch();
}
Pointers To Derived Class
C++ allows a base pointer to point any object
derived from that base class but we cannot
directly use the pointer to access the all the
members of derived class. Another pointer
might be required as a pointer to the derived
class.
#include<iostream.h>
#include<conio.h>
class base
{
public:
int x,y;
void input()
{
cin>>x>>y;
}
void add()
{
cout<<x+y;
}
};
class derived:public base
{
public:
int z;
void enter()
{
cin>>z;
}
void sum()
{
cout<<x+y+z;
}
};
void main()
{
clrscr();
base *ptr1;
base o1;
ptr1=&o1;
ptr1->input();
ptr1->add();
derived *ptr2;
derived o2;
ptr2=&o2;
ptr2->input();
ptr2->enter();
ptr2->sum();
getch();
}
Static And Dynamic Memory
Allocation
Static Memory Allocation
When the memory to be allocated is known in
advance and it is allocated during compilation,
it is called as static memory allocation.
For Example:
Int x;
Allocates 2 bytes of main memory during
compilation.
Dynamic Memory Allocation
When the amount of memory to be allocated is
not known in advance and it is required at
execution time, it is called as dynamic
memory allocation.
There are two operators new and delete for
dynamic memory allocation.
Free Store
Every program in C++ is provided with a pool of
unallocated heap memory for utilization
during run time, this pool is known as
programs free store.
New Operator
The operator new is used for memory allocation
dynamically.
The syntax of new operator is given below:
Pointer_variable= new data_type

Example:
Char *cptr;
Cptr= new char;
The above statement allocates 1 byte and assigns
the address to cptr.
Delete Operator
The operator delete is used for deallocation,
when the memory is no longer required.
The syntax of delete operator:
Delete cptr;
#include<iostream.h>
#include<conio.h>
int *empno;
float * salary;
void main()
{
int size;
clrscr();
cout<<"How many employees are there" ;
cin>>size;
empno= new int[size];
salary=new float[size];
cout<<"Enter the data";
for(int i=0;i<size;i++)
{
cout<<"Enter employee number and salary of employee";
cin>>empno[i]>>salary[i];
}
cout<<"Entered data is";
for(i=0;i<size;i++)
{
cout<<"Employee number"<<empno[i];
cout<<"Salary is"<<salary[i];
}
delete[] empno;
delete[]salary;
getch();
}
Memory Leaks
If we forget to delete the reserved memory
allocated using new, an orphand memory
block: a bock that is still allocated but there is
nothing referencing it, still exists. A function
that allocates the memory to some object
dynamically but does not deallocate it,
consumes some space every time it is
executed and has a bad effect on the system.
This situation is called as memory leak.
Reasons for Leading Memory Leaks
1. Forgetting the deletion of some dynamically
allocated objetcs.
2. Bypassing the delete statement in certain
cases.

Vous aimerez peut-être aussi