C/C++

new and delete operator in C++ with examples

new and delete operator in c++

new Operator:

new and delete operator in c++:- the new operator is used to allocating memory location during program execution. This operator allocates memory for the specified object and returns its memory address (i.e. starting address of the allocated memory) to the pointer variable. The allocated memory has n name. the data into the allocated memory location is accessed with the reference of the pointer. The new operator can be used to allocate memory for simple variables, arrays, or user-defined objects, etc.

The general syntax of the new operator is as for follows:

ptr= new data_type;

Where

ptr: it represents the pointer to an object.

Data_type: it represents the data type. It may be the standard data type or name of a defined class or structure. It must be the same as type of pointer variable.

For example:

float *p;

p=new float;

In these statements, the first statement declares a pointer variable of float type. In the second statement, the new operator allocates memory of size 4 bytes to store float type data. The address of allocated memory is assigned to pointer ā€˜pā€™.

new and delete operator in c++



We can also allocate a consecutive block of memory to store same type of data such as an array. In this case, the memory address of first element of array is assigned to the pointer variable. For example, to allocate memory for n array of ten elements, the statements are written as:

Int *p;

P= new int[10];

new and delete operator in c++

delete operator:

Once the memory is allocated with the new operator, it cannot be de-allocated automatically. It the pointer (that points to the allocated memory) goes out the scope, the allocated memory becomes unavailable.

The delete operator is used to de-allocated memory occupied by an object. The de-allocated memory can be used by the operating system for other purposes.

The general syntax of delete operator to de-allocated memory occupied by an array is as follows:

delete[] ptr;

Where

ptr: it represents the pointer variable which holds the memory address of the object which is to deleted from memory.

Programming Examples:

How to use new and delete operator in C++ that allocates memory location for two integer values and computes the sum of these values:

#include <iostream>
using namespace std;

int main()
{
   int *p1, *p2, sum;
   p1=new int;
   p2= new int;
   cout<<" Enter first value :";
   cin>>*p1;
    cout<<" Enter second value :";
   cin>>*p2;
   sum = *p1 + *p2;
   cout<<" Sum of values = "<<sum<<endl;
   delete p1;
   delete p2;
}

new and delete operator in c++


How to use new and delete operator in C++ that allocates memory location for an array of int type and find the largest value in the array:

#include <iostream>
using namespace std;

int main()
{
   float *p1, *p2, max;
   int n, i;
   cout<<" Enter the size of array :";
   cin>>n;
   p1=new float[n];
   p2=p1;
   cout<<" Enter "<<n<<" values in array\n";
   for(i=0; i<n; i++)
    cin>>*p1++;
    i=0;
    p1=p2;
    max = *p1;
    while(i<n)
    {
        if(max<*p1)
            max= *p1;
        *p1++;
        i++;
        
    }
    cout<<" the largest value is :"<<max;
    delete p1;

}

new and delete operator in c++

How to use new and delete operator in C++ that allocates memory location for four integer values and store the addresses of allocated memory and find out the smallest value from the four values:

#include <iostream>

using namespace std;

int main()
{
   int *p[4], *pn1, *pn2, *pn3, *pn4, min, i;
   pn1= new int;
   cout<<" enter first value :";
   cin>>*pn1;
   pn2 = new int;
   cout<<" enter second value :";
   cin>>*pn2;
   pn3 =new int;
   cout<<" enter third value :";
   cin>>*pn3;
   pn4= new int;
   cout<<" enter fourth value :";
   cin>>*pn4;
   p[0] = pn1;
   p[1] = pn2;
   p[2] = pn3;
   p[3] = pn4;
   min= *p[0];
   i = 1;
   while (i<=3)
   {
       if(min > *p[i])
            min = *p[i];
        i++;
        
   }
  
   cout<<" the smallest value is :"<<min;

      delete pn1;
   delete pn2;
   delete pn3;
   delete pn4;
  

}

new and delete operator in c++


Related Article:

Pointer Variables in C++ with Examples

Related Articles

One Comment

  1. there is an error in this code. You used delete p1, you should have used delete [] p1;

    #include
    using namespace std;

    int main()
    {
    float *p1, *p2, max;
    int n, i;
    cout<>n;
    p1=new float[n];
    p2=p1;
    cout<<" Enter "<<n<<" values in array\n";
    for(i=0; i>*p1++;
    i=0;
    p1=p2;
    max = *p1;
    while(i<n)
    {
    if(max<*p1)
    max= *p1;
    *p1++;
    i++;

    }
    cout<<" the largest value is :"<<max;
    delete p1;

    }

Leave a Reply

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

Back to top button