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

100 Python Programs for Beginner (49) AI (34) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (173) C (77) C# (12) C++ (82) Course (67) Coursera (226) Cybersecurity (24) data management (11) Data Science (128) Data Strucures (8) Deep Learning (20) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (34) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (59) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (3) Pandas (4) PHP (20) Projects (29) Python (929) Python Coding Challenge (351) Python Quiz (21) Python Tips (2) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (3) Software (17) SQL (42) UX Research (1) web application (8) Web development (2) web scraping (2)

Followers

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