Dynamic Memory Management in cpp.
In this tutorial we learn Dynamic Memory Management operator in cpp.This operator use to real time memory of the variable.Memory not allocated statically.This operator use only for Memory allocation and deallocation.
1)New :
This operator is use to newly allocate memory of any data type.
syntax:
pointer_variable=new data-type;
Here we use pointer-variable to allocate memory.new is the keyword and followed that data-type of data to store.The data type could be use any built-in data type.It also int user defined data type like structure and class.
Example:
p=new student;
2)delete:
This operator use to deallocate the memory.It free up the memory that as already allocate.
syntax:
delete variable-name;
This syntax use to delete the allocate memory.
example:
delete p;
here p is the object of class when this statement is executed then the memory for this object free up.
code:
#include<iostream.h>
#include<conio.h>
class student
{
char name[20];
int rno;
float avg;
public:
void getdata();
void putdata();
};
void student::getdata()
{
cout<<"\nEnter the name of student:";
cin>>name;
cout<<"\nEnter the roll number of student:";
cin>>rno;
cout<<"\nEnter average of student:";
cin>>avg;
}
void student::putdata()
{
cout<<"\n"<<name<<"\t"<<rno<<"\t"<<avg;
}
int main()
{
int n,i;
student *p,*q;
clrscr();
cout<<"\nEnter number of student:";
cin>>n;
p=new student[n];
q=p;
for(i=1;i<=n;i++)
{
cout<<"\nEnter "<<i<<" Student information";
p->getdata();
p++;
}
p=q;
cout<<"\nStudent information:";
cout<<"\nName\tRoll no\tAverage";
for(i=1;i<=n;i++)
{
p->putdata();
p++;
}
delete p;
getch();
return(0);
}
OUTPUT:
In this tutorial we learn Dynamic Memory Management operator in cpp.This operator use to real time memory of the variable.Memory not allocated statically.This operator use only for Memory allocation and deallocation.
1)New :
This operator is use to newly allocate memory of any data type.
syntax:
pointer_variable=new data-type;
Here we use pointer-variable to allocate memory.new is the keyword and followed that data-type of data to store.The data type could be use any built-in data type.It also int user defined data type like structure and class.
Example:
p=new student;
2)delete:
This operator use to deallocate the memory.It free up the memory that as already allocate.
syntax:
delete variable-name;
This syntax use to delete the allocate memory.
example:
delete p;
here p is the object of class when this statement is executed then the memory for this object free up.
code:
#include<iostream.h>
#include<conio.h>
class student
{
char name[20];
int rno;
float avg;
public:
void getdata();
void putdata();
};
void student::getdata()
{
cout<<"\nEnter the name of student:";
cin>>name;
cout<<"\nEnter the roll number of student:";
cin>>rno;
cout<<"\nEnter average of student:";
cin>>avg;
}
void student::putdata()
{
cout<<"\n"<<name<<"\t"<<rno<<"\t"<<avg;
}
int main()
{
int n,i;
student *p,*q;
clrscr();
cout<<"\nEnter number of student:";
cin>>n;
p=new student[n];
q=p;
for(i=1;i<=n;i++)
{
cout<<"\nEnter "<<i<<" Student information";
p->getdata();
p++;
}
p=q;
cout<<"\nStudent information:";
cout<<"\nName\tRoll no\tAverage";
for(i=1;i<=n;i++)
{
p->putdata();
p++;
}
delete p;
getch();
return(0);
}
OUTPUT:
No comments:
Post a Comment