POINTERS Pointers :
- Pointer is a variable that holds a memory address of another variable.
- It supports dynamic allocation routines.
- It can improve the efficiency of certain routines. C++ Memory Map :
- Program Code : It holds the compiled code of the program.
- Global Variables : They remain in the memory as long as program continues.
- Stack : It is used for holding return addresses at function calls, arguments passed to the functions, local variables for functions. It also stores the current state of the CPU.
- Heap : It is a region of free memory from which chunks of memory are allocated via DMA functions.
Static Memory Allocation : The amount of memory to be allocated is known in advance and it allocated during compilation, it is referred to as Static Memory Allocation.
Eg. Int a; // This will allocate 2 bytes for a during compilation. Dynamic Memory Allocation : The amount of memory to be allocated is not known beforehand rather it is required to allocated as and when required during runtime, it is referred to as dynamic memory allocation.
C++ offers two operator for DMA – new and delete
Q) Find the output of the following program:
#include<iostream.h>
void main()
{ int Array[]={4,6,10,12};
int *pointer=Array;
for(int I=1;I<=3;I++)
{ cout<<*pointer<<”#”;
pointer++;
}
cout<<endl;
for(I=1;I<=4;I++)
{ (*pointer)*=3;
--pointer;
}
for(I=1;I<5;I++)
cout<<Array[I-1]<<”@”;
cout<<endl;
}
Q) Find the output of the following program:
#include<iostream.h>
void main( )
{ int Numbers[]={2,4,8,10};
int *ptr=Numbers;
for(int C=1;C<3;C++)
{ cout<<*ptr<<”@”;
ptr++;
}
cout<<endl;
for(C=0;C<4;C++)
{ (*ptr)*=2;
--ptr;
}
for(C=0;C<4;C++)
cout<<Numbers[C]<<”#”;
cout<<endl; }
Q) Find the output of the following program:
#include<iostream.h>
#include<string.h>
class state
{ char *state_name;
int size;
public:
state( )
{ size=0;
state_name=new char[size+1];
}
state(char *s)
{ size=strlen(s);
state_name=new char[size+1];
strcpy(state_name,s);
}
void display( )
{ cout<<state_name<<endl;
}
void Replace(state &a, state &b)
{ size=a.size+b.size;
delete state_name;
state_name=new char[size+1];
strcpy(state_name,a.state_name);
strcat(state_name,b.state_name);
}
};
void main( )
{ char *temp=”Delhi”;
state
state1(temp),state2(“Mumbai”),state3(“Nagpur”),S1,S2;
S1.Replace(state1,state2);
S2.Replace(S1,state3);
S1.display( );
S2.display( );}
Q) Find the output of the following program:
#include<iostream.h>
#include<string.h>
class student
{ char *name;
int I;
public:
student( )
{ I=0;
name=new char[I+1];
}
student(char *s) { I=strlen(s);
name=new char[I+1];
strcpy(name,s); }
void display( )
{ cout<<name<<endl; }
void manipulate(student &a, student &b)
{
I=a.I+b.I;
delete name;
name=new char[I+1];
strcpy(name,a.name);
strcat(name,b.name);
}
};
void main( )
{ char *temp=”Jack”;
Student name1(temp),name2(“Jill”),name3 (“John”) ,S1,S2;
S1.manipulate(name1,name2);
S2.manipulate(S1,name3);
S1.display( );S2.display( ); }
Q) What is “this” pointer? Give an example to illustrate the use of it in C++.
Answer: A special pointer known as this pointer stores the address of the object that is currently invoking a member function. The this pointer is implicitly passed to the member functions of a class whenever they are invoked. (As soon as you define a class, the member functions are created and placed in the memory space only once.
That is, only one copy of member functions is maintained that is shared by all the objects of the class. Only space for data members is allocated separately for each object.When a member function is called, it is automatically passed an implicit(in built) argument that is a pointer to the object that invoked the function. This pointer is called this. If an object is invoking a member function, then an implicit argument is passed to that member function that points to (that) object. The programmer also can explicitly specify ‘this’ in the program if he desires.)
Eg: Example program to demonstrate the usage of this pointer.
#include<iostream.h>
#include<conio.h>
class Rectangle
{ float area,len,bre;
public:
void input( )
{ cout<<"\nEnter the length and breadth: ";
cin>>this->len>>this->bre;
}
void calculate( )
{ area=len*bre;
//Here Implicit 'this' pointer will be worked.
}
void output( )
{
cout<<"\nThe Area of the Rectangle: "<<this->area;
}
};
void main( )
{
Rectangle R;
clrscr( );
R.input( );
R.calculate( );
R.output( );
getch();
}
Q) What will be the output of the following program:
#include<iostream.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>
void ChangeString(char Text[],int&Counter)
{ char *Ptr=Text;
int Length=strlen(Text);
for(;Counter<Length- 2;
Counter+=2,Ptr++)
{
*(Ptr+Counter)=toupper(*(Ptr+Counter));
}
}
void main( )
{ clrscr( );
int Position=0;
char Message[]=”Pointers Fun”;
ChangeString(Message,Position);
cout<<Message<<”@”<<Position;
}
Q) Identify the syntax error(s), if any, in the following program. Also give reason for errors.
Void main()
{const int i=20;
const int* const ptr=&i;
(*ptr)++;
int j=15;
ptr=&j;
}
Answer:
Error Line 5 : Cannot modify a const object.
Error Line 7 : Cannot modify a const object.Warning Line 8 : ‘j’ is assigned a value that is never used.Warning Line 8 : ‘ptr’ is assigned a value that is never used.
Explonation:
(1) Error 1 is in Line no.5 ie (*ptr)++. Here ptr is a constant pointer ie the contents cann’t be modified.
(2) Error 2 is in Line no.7 ie ptr=&j;.Here ptr is a constant pointer the address in this pointer can’t be modified. (It is already pointing the address of i.)
Q) Give the output of the following program segment. (Assuming all required header files are included in the program).
void main( )
{ int a=32,*x=&a;
char ch=65,&cho=ch;
cho+=a;
*x+=ch;
cout<<a<<’,’<<ch<<endl; }
Q) Distinguish between
int *ptr=new int(5);
int *ptr=new int[5];
Answer: The int *ptr=new int(5); declares and creates the space for the new data directly.Ie The new operator reserves 2 bytes of memory from heap memory (free pool) and returns the address of that memory location to a pointer variable called ptr, 5 is the initial value to be stored in the newly allocated memory.
The int *ptr = new int[5]; initializes an array element. A memory space for an integer type of array having 5 elements will be created from the heap memory (free pool).
Q) Give the output of the following program:
#include<iostream.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<<”\n”;
cout<<”Salary:“<<salary<<”\n”;
}
};
void main( )
{ Per P1(“REEMA”,10000),
P2(“KRISHNAN”,20000),
P3(“GEORGE”,5000);
per *P;
P=P1.GR(P3);P->display( );
P=P2.GR(P3);P->display( ); }
Q) Give the output of the following program.
#include<stdio.h>
void main( )
{ char *p=”Difficult”;
char c; c=*p++; cout<<c;
}