The new & delete operators of C++ provides a nicer means of performing Dynamic Memory Allocation

(for any built-in or user-defined type) than C’s malloc() and free() functions calls.

The new operator allocated memory free store (in the C++ terminology, the heap is called free store), whereas, the delete operator returns the allocated memory back to the free store.

The new operator

An object is created using the new operator and when it is created inside a block, it remains to exist till it is explicitly destroyed using the delete operator.

Hence, the programmer can control the lifetime of the object and it does not depend upon the block structure of the program objects of any type can be created using the new operator

Syntax:

pointer_variable = new data_type;

The pointer variable is of the type data_type. The new operator allocates sufficient memory to hold a data object of the type data_type and also returns the address of the object created.

The data_type can be of any of the valid basic datatypes. The address of the memory space allocated is stored in the pointer_variable.

The delete operator

Memory allocated using new[] for user-defined objects must be de-allocated using delete[].

When we no longer need a data object, we can destroy it and release the memory space for reuse by other data objects.

Syntax:

deletepointer_variable;

The pointer_variables the pointer that points to the data object that was created with a new operator.

Whenever we need to free the memory allocated for an array then, we can do so as follows: delete [size] pointer_variable The size specifies the number of elements that need to be freed in the array.

The challenge in using the delete operator with arrays is that the programmer needs to remember the size of the array.

If we need to free the entire array, the size need not be specified.

Simple Program For New and Delete Operator in C++ program

#include<iostream>
using namespace std;
int main(){ 
int a, n, i; 
cout<< "How many numbers do you want to enter :"; 
cin>>n; 
a=new int[n]; //or a = new int(n); // memory allocated 
cout<< "n Enter numbers:"; 
for(i=0; i<n; i++){ 
cin>>a[i]; // or cin>>(a+i);
} cout<<"n The entered number are :";
for(i=0; i<n; i++){ 
cout<<"n " <<a[i]; //or cout<<*(a+i)<<endl;
} delete a;
return 0; }

Output:-How many numbers do you want to enter :5
Enter numbers: 22 23 24 25 26
The entered number are :
22
23
24
25
26

Explanation of the program

Instead of using the new operator to allocate memory, what would have happened if we had used the malloc() the allocation statements??

The program would have looked like this: a = new (int*)malloc(n* sizeof(int) ); Note that since malloc() returns a void pointer.

it is necessary to typecast it into an appropriate type depending on the type of pointer we have on the left side of the = operator. This gets completely avoided when we use the new operator.

This article is written by our awesome writer
Comments to: New and Delete Operator in C++ with example

Your email address will not be published. Required fields are marked *

Attach images - Only PNG, JPG, JPEG and GIF are supported.

New Dark Mode Is Here

Sign In to access the new Dark Mode reading option.

Join our Newsletter

Get our monthly recap with the latest news, articles and resources.

By subscribing you agree to our Privacy Policy.

Latest Articles

Explore Tutorials By Categories

About

Codeverb is simply an all in one interactive learning portal, we regularly add new topics and keep improving the existing ones, if you have any suggestions, questions, bugs issue or any other queries you can simply reach us via the contact page

Login

Welcome to Codeverb

Ready to learn something new?
Join Codeverb!

Read Smart, Save Time
  •  
    Strength indicator
  •  
  •  
    Log In | Lost Password