Memory Management Operators With Program and Examples

Memory management operators, New Operator, Delete Operator explain with Examples and Program


Memory management operators :-


Memory management means the way of allocating part of a memory to program at their request, and freeing it for reuse when no longer needed. In C++ unary operators ‘new’ and ‘delete’ are used to allocate and free the dynamic memory, they are also called as free store operators. 

new:
            Pointer variable = new data type;
                Here pointers variable is a dynamic variable of type pointer. 
The unary operator new is used to allocate required memory at runtime. There are three stages to allocate required memory they are

1.    Locates and reserves storage for the object or objects to be allocated.
2.    Initializes the object(s) using class constructor
3.    Returns a pointer to the object(s) of a pointer type derived from type-name.

The ‘new’ operator allocates sufficient memory to hold data and return its address. If the object is of type array it returns the base address of an array. The memory allocated by using new operator is not initialized.  If the required amount of memory is not allocated the operator new throws an exception to set_new_handler() function. The memory allocated by using new operator
Ex: 1) int *p,*q;
            p= new int;
            q= new int;
        We can combine the declaration of pointers and their assignments as follows.
        2)     int *p = new int;
            float *x = new float;
         Now we can use the above pointers as follows.
            *p =15;        *x = 18.9;
        We can also initialize the memory using the new operator as follows
        3) int *p = new int (10);
        Thus *p will contain value ‘10’
        We can also create one-dimensional array by using new operator as follows.
        4) int *p = new int [10];
    The above statement creates a memory space for an array of 10 integers hence p[0] will refer the first element. p[1] to the second element and so on.


Delete:
When a data object is no longer needed, it is destroyed to release the memory space for reuse. The unary operator delete is used to release the memory allotted by using new operator. The delete operator does not return any value.  When delete is used to deallocate memory for a C++ class object, the object's destructor is called before the object's memory is deallocated (if the object has a destructor).  The general form of delete operator is
   
delete (ptrvarible);  Where ptrvaribale is having a dynamic memory allocated using new

For example:
    delete P - If P is a pointer variable then it will be destroyed after executing this statement

To free a dynamically allocated array by using    
    delete [size] pointer variable; the size specifies the Number of elements in the array to be free

To free all the allocated memory location of an array by using
    delete[](pointer variable) In this case it is not required to give the size of the array.
At hear the [ ] tell the compiler the released memory is of type pointer. Only one pair of [ ] are sufficient to release memory for any dimension.

Example:
#include<iostream.h>
#include<conio.h>
void main()
{     clrscr();
      int *p_i=new int;
      float  *p_f=new float;
      char *p_c=new char;
      int *p_a=new int[5];
      cout<<"\nEnter any integer value:";
      cin>>*p_i;
       cout<<"\nEnter any float value:";
      cin>>*p_f;
      cout<<"\nEnter any characte value:";
      cin>>*p_c;
      cout<<"\nenter aaray elements:";
      for(int i=0;i<5;i++)
      cin>>p_a[i];
      cout<<" \ninteger value is:"<<*p_i;
      cout<<" \nfloat value is:"<<*p_f;
      cout<<" \ncharacter value is:"<<*p_c;
      cout<<"\nelements are:";
      for(i=0;i<5;i++)
      cout<<p_a[i];
      delete p_i;
      delete p_f;
      delete p_c;
      delete p_a;
      getch();
      }





Labels: