C++ Tutorial/Development/delete

Материал из C\C++ эксперт
Версия от 10:28, 25 мая 2010; Admin (обсуждение | вклад) (1 версия: Импорт контента...)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Delete an array of objects

#include <iostream>
#include <cstdlib>
#include <new>
using namespace std;
class Point {
  int x, y;
public:
  Point() {x = y = 0;}
  Point(int px, int py) {
    x = px;
    y = py;
  }
  void show() {
    cout << x << " ";
    cout << y << "\n";
  }
  void *operator new(size_t size);
  void operator delete(void *p);
  void *operator new[](size_t size);
  void operator delete[](void *p);
};
// new overloaded relative to Point.
void *Point::operator new(size_t size)
{
void *p;
  cout << "In overloaded new.\n";
  p =  malloc(size);
  if(!p) {
    bad_alloc ba;
    throw ba;
  }
  return p;
}
// delete overloaded relative to Point.
void Point::operator delete(void *p)
{
  cout << "In overloaded delete.\n";
  free(p);
}
// new overloaded for Point arrays.
void *Point::operator new[](size_t size)
{
  void *p;
  cout << "Using overload new[].\n";
  p =  malloc(size);
  if(!p) {
    bad_alloc ba;
    throw ba;
  }
  return p;
}
// delete overloaded for Point arrays.
void Point::operator delete[](void *p)
{
  cout << "Freeing array using overloaded delete[]\n";
  free(p);
}
int main()
{
  Point *p1, *p2;
  int i;
  try {
    p1 = new Point (10, 20); // allocate an object
  } catch (bad_alloc xa) {
    cout << "Allocation error for p1.\n";
    return 1;;
  }
  try {
    p2 = new Point [10]; // allocate an array
  } catch (bad_alloc xa) {
    cout << "Allocation error for p2.\n";
    return 1;;
  }
  p1->show();
  for(i=0; i<10; i++)
    p2[i].show();
  delete p1; // free an object
  delete [] p2; // free an array
  return 0;
}
In overloaded new.
Using overload new[].
10 20
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
In overloaded delete.
Freeing array using overloaded delete[]

delete class array and return memory to heap

#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

Global delete

#include <iostream>
#include <cstdlib>
#include <new>
using namespace std;
class Point {
  int x, y;
public:
  Point() {}
  Point(int px, int py) {
    x = px;
    y = py;
  }
  void show() {
    cout << x << " ";
    cout << y << "\n";
  }
};
// Global new
void *operator new(size_t size)
{
  void *p;
  p =  malloc(size);
  if(!p) {
    bad_alloc ba;
    throw ba;
  }
  return p;
}
// Global delete
void operator delete(void *p)
{
  free(p);
}
int main()
{
  Point *p1, *p2;
  float *f;
  try {
    p1 = new Point (10, 20);
  } catch (bad_alloc xa) {
    cout << "Allocation error for p1.\n";
    return 1;;
  }
  try {
    p2 = new Point (-10, -20);
  } catch (bad_alloc xa) {
    cout << "Allocation error for p2.\n";
    return 1;;
  }
  try {
    f = new float; // uses overloaded new, too
  } catch (bad_alloc xa) {
    cout << "Allocation error for f.\n";
    return 1;;
  }
  *f = 10.10F;
  cout << *f << "\n";
  p1->show();
  p2->show();
  delete p1;
  delete p2;
  delete f;
  return 0;
}
10.1
10 20
-10 -20

Memory management new-delete

#include <iostream.h>
#include <stdlib.h>
int main ()
{
  char input [100];
  int count,n;
  long * longPointer, total = 0;
  
  cout << "How many numbers do you want to type in? ";
  
  cin.getline (input,100); 
  count=atoi (input);
  
  longPointer= new long[count];
  
  if (longPointer == NULL) 
      exit (1);
  
  for (n=0; n<count; n++)
  {
    cout << "Enter number: ";
    cin.getline (input,100); 
    longPointer[n]=atol (input);
  }
  cout << "You have entered: ";
  for (n=0; n<count; n++)
    cout << longPointer[n] << ", ";
  delete[] longPointer;
  return 0;
}
How many numbers do you want to type in? 1
Enter number: a
You have entered: 0, "

object in existence after deletion

#include<iostream.h>
class MyClass
{
  static int total;
public:
  MyClass()
  {
    total++;
  }
  ~MyClass()
  {
    total--;
  }
  int gettotal()
  {
    return total;
  }
};
int MyClass::total=0;
main()
{
  MyClass o1,o2,o3;
  cout<<o1.gettotal()<<" objects in existence\n";
  MyClass *p;
  p=new MyClass;
  if(!p)
  {
    cout<<"Allocation erroe\n";
       return 1;
  }
  cout<<o1.gettotal();
  cout<<" object in existence after allocation\n";
  delete p;
  cout<<o1.gettotal();
  cout<<" object in existence after deletion\n";
  return 0;
}
3 objects in existence
4 object in existence after allocation
3 object in existence after deletion

Using new and delete of array

#include <iostream.h>
class Point
{
    int x;
    int y;
public:
   Point(){ 
      x = 0; 
      y = 0;
   }
   
   Point(int a,int b=0)  { 
      x = a; 
      y= b; 
   }
   void Set (int a,int b=0) { 
      x = a; 
      y = b; 
   }
   void Display() {
        cout << "x = "<< x << " y = "<< y   << endl;
   }
   ~Point() {
        cout << "Destruct obj.\n" ;
   }
};
int main()
{
    Point *p;int i;
    const int Length = 3;
    p=new Point[Length];
    
    if(!p)
    {  
        cout << "allocation failure\n";  
        return -1;  
    }
    for (i=0;i<Length;i++)
        p[i].Set(i*10,-i*10);
    for (i=0;i<Length;i++)
        p[i].Display();
    delete []p;
    return 0;
}
x = 0 y = 0
x = 10 y = -10
x = 20 y = -20
Destruct obj.
Destruct obj.
Destruct obj.