delete class array and return memory to heap : delete « Development « C++ Tutorial






#include<iostream.h>

class examp
{
  int i,j;
public:
  void init(int a,int b){
    i=a,j=b;
  }
  int product()  {
    return i*j;
  }
};

main()
{
  examp *ptr;
  int i;
  ptr=new examp[6];
  if(!ptr)
  {
    cout<<"Allocation error.\n";
       return 1;
  }
  for(i=0;i<6;i++)
     ptr[i].init(i,i);
  
  for(i=0;i<6;i++)
  {
     cout<<"Product["<<i<<"]is:";
        cout<<ptr[i].product()<<"\n";
  }
  delete[]ptr;
  return 0;
}
Product[0]is:0
Product[1]is:1
Product[2]is:4
Product[3]is:9
Product[4]is:16
Product[5]is:25








5.13.delete
5.13.1.Memory management new-delete
5.13.2.Delete an array of objects
5.13.3.Global delete
5.13.4.object in existence after deletion
5.13.5.delete class array and return memory to heap
5.13.6.Using new and delete of array