Académique Documents
Professionnel Documents
Culture Documents
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
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.