C++/Overload/New Delete
Define new.delete/new[]/delete[] operators
#include <iostream>
#include <stdlib.h>
using namespace std;
class loc
{
int longitude, latitude;
public:
loc(void) {}
~loc(void);
loc (int lg, int lt)
{
longitude = lg;
latitude = lt;
}
void show(void)
{
cout << longitude << " ";
cout << latitude << endl;
}
void *operator new(size_t size);
void operator delete(void *p);
void *operator new[](size_t size);
void operator delete[](void *p);
};
loc::~loc(void)
{
cout << "In the destructor function" << endl;
}
void *loc::operator new(size_t size)
{
cout << "In the custom new operator function." << endl;
return malloc(size);
}
void loc::operator delete(void *p)
{
cout << "In the custom delete operator function." << endl;
free(p);
}
void *loc::operator new[](size_t size)
{
cout << "In the custom new ARRAY allocator function." << endl;
return malloc(size);
}
void loc::operator delete[](void *p)
{
cout << "Freeing the ARRAY in the custom delete function." << endl;
free(p);
}
int main(void)
{
loc *p1, *p2;
int i;
p1 = new loc(10,20);
if (!p1)
{
cout << "Allocation error\n";
exit(1);
}
p2 = new loc[10];
if (!p2)
{
cout << "Allocation error\n";
exit(1);
}
p1->show();
for(i=0; i<10; i++)
p2[i].show();
delete p1;
delete [] p2;
}
Demonstrate overloaded new and delete.
#include <iostream>
#include <new>
#include <cstdlib>
using namespace std;
class MyClass {
int x, y, z;
public:
MyClass() {
x = y = z = 0;
cout << "Constructing 0, 0, 0\n";
}
MyClass(int i, int j, int k) {
x = i;
y = j;
z = k;
cout << "Constructing " << i << ", ";
cout << j << ", " << k;
cout << "\n";
}
~MyClass( ) {
cout << "Destructing\n";
}
void *operator new(size_t size);
void *operator new[](size_t size);
void operator delete(void *p);
void operator delete[](void *p);
void show() ;
};
void *MyClass::operator new(size_t size)
{
void *p;
cout << "Allocating MyClass object.\n";
p = malloc(size);
if(!p) {
bad_alloc ba;
throw ba;
}
return p;
}
void *MyClass::operator new[](size_t size)
{
void *p;
cout << "Allocating array of MyClass objects.\n";
p = malloc(size);
if(!p) {
bad_alloc ba;
throw ba;
}
return p;
}
void MyClass::operator delete(void *p)
{
cout << "Deleting MyClass object.\n";
free(p);
}
void MyClass::operator delete[](void *p)
{
cout << "Deleting array of MyClass objects.\n";
free(p);
}
void MyClass::show()
{
cout << x << ", ";
cout << y << ", ";
cout << z << endl;
}
int main()
{
MyClass *objectPointer1, *objectPointer2;
try {
objectPointer1 = new MyClass[3]; // allocate array
objectPointer2 = new MyClass(5, 6, 7); // allocate object
} catch (bad_alloc ba) {
cout << "Allocation error.\n";
return 1;
}
objectPointer1[1].show();
objectPointer2->show();
delete [] objectPointer1; // delete array
delete objectPointer2; // delete object
return 0;
}
Overload new, new[], delete, and delete[] for the three_d class.
#include <iostream>
#include <cstdlib>
#include <new>
using namespace std;
class three_d {
int x, y, z; // 3-D coordinates
public:
three_d() { x = y = z = 0; }
three_d(int i, int j, int k) { x = i; y = j; z = k; }
// Set the coordinates of an object after it is created.
void set(int i, int j, int k) { x = i; y = j; z = k; }
// Overload new and delete for three_d objects.
void *operator new(size_t size);
void operator delete(void *p);
// Overload new[] and delete[] for three_d arrays.
void *operator new[](size_t size);
void operator delete[](void *p);
// Let the overloaded inserter be a friend.
friend ostream &operator<<(ostream &strm, three_d op);
};
// The three_d inserter is a non-member operator function.
ostream &operator<<(ostream &strm, three_d op) {
strm << op.x << ", " << op.y << ", " << op.z << endl;
return strm;
}
// Overload new for three_d.
void *three_d::operator new(size_t size)
{
void *p;
cout << "Using overloaded new for three_d.\n";
p = malloc(size);
if(!p) {
bad_alloc ba;
throw ba;
}
return p;
}
// Overload delete for three_d.
void three_d::operator delete(void *p)
{
cout << "Using overloaded delete for three_d.\n";
free(p);
}
// Overload new[] for three_d arrays.
void *three_d::operator new[](size_t size)
{
void *p;
cout << "Using overloaded new[] for three_d.\n";
p = malloc(size);
if(!p) {
bad_alloc ba;
throw ba;
}
return p;
}
// Overload delete[] for three_d arrays.
void three_d::operator delete[](void *p)
{
cout << "Using overloaded delete[] for three_d.\n";
free(p);
}
int main()
{
three_d *p1, *p2;
int i;
// Allocate a three_d object.
try {
p1 = new three_d (10, 20, 30);
} catch (bad_alloc xa) {
cout << "Allocation error for p1.\n";
return 1;
}
delete p1;
try {
p2 = new three_d [10];
} catch (bad_alloc xa) {
cout << "Allocation error for p2.\n";
return 1;
}
// Assign coordinates to three of p2"s elements.
p2[1].set(9, 8, 7);
p2[5].set(-1, -2, -3);
p2[8].set(6, 7, 8);
cout << "Contents of a dynamic three_d array:\n";
for(i=0; i<10; i++) cout << p2[i];
delete [] p2;
return 0;
}