Wednesday 4 April 2018

'new' and 'delete' Operators

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

0 Comments:

Post a Comment

Popular Posts

Categories

AI (27) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (113) C (77) C# (12) C++ (82) Course (60) Coursera (176) coursewra (1) Cybersecurity (22) data management (11) Data Science (85) Data Strucures (6) Deep Learning (9) Django (6) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (5) flutter (1) FPL (17) Google (18) Hadoop (3) HTML&CSS (46) IBM (25) IoT (1) IS (25) Java (92) Leet Code (4) Machine Learning (43) Meta (18) MICHIGAN (4) microsoft (3) Pandas (3) PHP (20) Projects (29) Python (726) Python Coding Challenge (169) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (40) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses