In 'C' , we have used malloc( ), calloc( ), and realloc( ) function to allocate memory dynamically that is at the run time. 'C++' also provides these function but other than this it supports new unary operators 'new' and 'delete' to allocate and to free memory at run time in a better and easier way than the above functions.
Advantages of using 'new' an 'delete' over above functions are
1. It automatically computers the size of the data object. So no need to computer the use sizeof( ) operator.
2. It automatically returns the correct pointer type , so that it is not need to use type casting.
3. Like any other operator , new and delete can be overloaded.
The 'new' operator can be used to create object of any data type. It has the following general form
pointer_variable = new data_type;
Here pointer_variable is a pointer of type data_type. The pointer_variable holds the address of the memory space allocated.
The data_type may be any valid data_type.
Ex:
int *p;
p = new int;
The 'new' operator can be used to create memory space for any data type including user define data type , such as array , structure and class.
The general form of one dimensional array is as follows.
pointer_variable = new data_type[size];
Here size gives information about the number of elements in the Array
Ex int foo
foo = new int[5];
In this case, the system dynamically allocates a space for five elements of type of int and returns a pointer to the first element of the sequence.
⬜⬜⬜⬜⬜⬜⬜⬜
↑
foo
Here foo is a pointer variable and thus the first element pointed by foo can be accessed either the expression foo[0] or *foo an second element as foo[1] or *(foo+1).
Note:
There is a substantial difference between declaring a normal array and allocating dynamic memory for a block of memory using new. The most important difference is that the size of a regular array needs to be constant expression and thus the size has to be determined at the moment of designing the program before it is run where as dynamic memory allocation performed by 'new' allows to assign memory during runtime using any variable value as size
'delete' Operator
Syntax:
delete pointer;
Delete[ ] pointer;
The first statement releases the memory of a single element allocated using new and second one releases the memory allocated for array of element using 'new'.
#include<conio.h>
#include<iostream.h?
void main( )
{
int i,n;
int *p;
clrscr( );
cout<<"\nHow many elements you want to store ?";
cin>>n;
p = new int[n];
cout<<"\nEnter elements in Array";
for(i=0;i<n;i++)
{
cin>>p[i];
}
cout<<"\nYou have elements";
for(i=0;i<n;i++)
{
cout<<"\t"<<((p+i);
}
cout<<"\nHow many elements you want to store ?";
cin>>n
p = new int[n];
cout<<"\nEnter elements in Array";
for(i=0;i<n;i++)
{
cin>>p[i];
}
cout<<"\nYou have elements";
for(i=0;i<n;i++)
{
cout<<"\t"<<*(p+i);
}
delete[ ]p; .//Deallocating memory
getch( );
}
Output:-
How many elements you want to store ?4
Enter elements in Array 1 2 3 4
You have element 1 2 3 4
How many elements you want to store ?6
Enter elements in Array 6 5 4 3 2 1
You have 6 5 4 3 2 1
Advantages of using 'new' an 'delete' over above functions are
1. It automatically computers the size of the data object. So no need to computer the use sizeof( ) operator.
2. It automatically returns the correct pointer type , so that it is not need to use type casting.
3. Like any other operator , new and delete can be overloaded.
The 'new' operator can be used to create object of any data type. It has the following general form
pointer_variable = new data_type;
Here pointer_variable is a pointer of type data_type. The pointer_variable holds the address of the memory space allocated.
The data_type may be any valid data_type.
Ex:
int *p;
p = new int;
The 'new' operator can be used to create memory space for any data type including user define data type , such as array , structure and class.
The general form of one dimensional array is as follows.
pointer_variable = new data_type[size];
Here size gives information about the number of elements in the Array
Ex int foo
foo = new int[5];
In this case, the system dynamically allocates a space for five elements of type of int and returns a pointer to the first element of the sequence.
⬜⬜⬜⬜⬜⬜⬜⬜
↑
foo
Here foo is a pointer variable and thus the first element pointed by foo can be accessed either the expression foo[0] or *foo an second element as foo[1] or *(foo+1).
Note:
There is a substantial difference between declaring a normal array and allocating dynamic memory for a block of memory using new. The most important difference is that the size of a regular array needs to be constant expression and thus the size has to be determined at the moment of designing the program before it is run where as dynamic memory allocation performed by 'new' allows to assign memory during runtime using any variable value as size
'delete' Operator
Syntax:
delete pointer;
Delete[ ] pointer;
The first statement releases the memory of a single element allocated using new and second one releases the memory allocated for array of element using 'new'.
#include<conio.h>
#include<iostream.h?
void main( )
{
int i,n;
int *p;
clrscr( );
cout<<"\nHow many elements you want to store ?";
cin>>n;
p = new int[n];
cout<<"\nEnter elements in Array";
for(i=0;i<n;i++)
{
cin>>p[i];
}
cout<<"\nYou have elements";
for(i=0;i<n;i++)
{
cout<<"\t"<<((p+i);
}
cout<<"\nHow many elements you want to store ?";
cin>>n
p = new int[n];
cout<<"\nEnter elements in Array";
for(i=0;i<n;i++)
{
cin>>p[i];
}
cout<<"\nYou have elements";
for(i=0;i<n;i++)
{
cout<<"\t"<<*(p+i);
}
delete[ ]p; .//Deallocating memory
getch( );
}
Output:-
How many elements you want to store ?4
Enter elements in Array 1 2 3 4
You have element 1 2 3 4
How many elements you want to store ?6
Enter elements in Array 6 5 4 3 2 1
You have 6 5 4 3 2 1
0 Comments:
Post a Comment