Monday, 6 May 2013

Dynamic memory allocation in C++


Dynamic Memory allocation: new and delete:

Run time memory can be allocated by using keyword new and this allocated memory can be released using keyword delete.

Example
#include<iostream.h>
#include<conio.h>
void main()
{
     clrscr();
     int *p= new int;
     float *q= new float;
     *p=5;
     *q=31.45;
     cout<<”*p=”<<*p;
     cout<<”*q=”<<*q;
     delete p;
     delete q;
     getch();
}

In the above program
l  Pointer p is used to store address of dynamically allocated int type block using keyword new.
l  Pointer q is used to store address of dynamically allocated float type block using keyword new.
l  In C language we dynamically allocate memory using predefined functions malloc() or calloc(), here same functionality is done by keyword new.
l  The only difference between malloc and new is in former case we have to specify the size of block and in later case we have to mention name of data type.
l  Dynamically allocated memory can be accessed only through pointers.
l  Memory can be released using keyword delete, this is same as function free().
l  Notice that delete releases the memory for specified address. delete p; would not mean that p is released instead it means delete releases memory pointed to by p.

Example
#include<iostream.h>
#include<conio.h>
void main()
{
     clrscr();
     int *p= new int[4];
     *p=5;
     *(p+1)=12;
     *(p+2)=33;
     *(p+3)=45;
     for(int i=0;i<=3;i++)
          cout<<”*(p+”<<i<<”)=”<<*(p+i);
    
delete []p;
     getch();
}

         In the above example
  • We created an array of 4 int blocks dynamically using keyword new.
  • Notice square brackets after data type int. This is used to mention sized of dynamic array.
  • Address of the first block of array gets stored in p. Subsequent instructions are used to store values in four blocks of array.
  • At last we use delete to release memory, see its syntax carefully, square brackets needs to be mentioned before pointer name. It makes possible to release memory of all four blocks.


Example:
#include<iostream.h>
#include<conio.h>
class complex
{
      private:
              int a,b;
      public:
             complex() {a=0; b=0;}
             void getdata()
             {
                  cout<<"Enter real and img part";
                  cin>>a>>b;
             }
             void show()
             {
                  cout<<"\na="<<a<<" b="<<b;
             }
};
void main()
{
     clrscr();
     complex *p= new complex;
     p->show();
     p->getdata();
     p->show();
     delete p;
     getch();
}

In the above example

  • We created object pointer to store address of dynamically allocated memory for object of type complex.
  • As soon as object is created constructor is called.
  • p->show()  display the values of a and b of this run time generated object.
  • Subsequent instructions are used to call other members of the class.
  •  Keyword delete releases the memory of object pointed to by p.

No comments:

Post a Comment