/*----------------------------------------------------- Script for AdSense -----------------------------------------------------*/ /* */ /* Footer ----------------------------------------------- */ #footer { clear: both; text-align: center; color: #333333; } #footer .widget { margin:.5em; padding-top: 20px; font-size: 85%; line-height: 1.5em; text-align: left; } /** Page structure tweaks for layout editor wireframe */ body#layout #header { width: 750px; } -->

Thursday, April 1, 2010

Overloading new and delete

From my diary:
Problem:
Was developing a galileoRx class where in many parameters need to allocate and free memory of different size. Naturally for one or two variables it is just a new and delete simple syntax code.
But, here many parameters, my code was not readable and on top of that everytime, size_t I need to give according to the array size for different variables.
This problem we can solve using operator overloading. (new and delete overloading)
I cannot give example of my own code here. (That is illegal)
But here, one sample code to explain how to do operator overloading using new and delete.
The skeletons for the functions that overload new and delete are shown here:


// Allocate an object.
void *operator new(size_t size)
{

/* Perform allocation. Throw bad_alloc on failure.
Constructor called automatically. */

return
pointer_to_memory;


}


// Delete an object.
void operator delete(void *p)
{

/* Free memory pointed to by p.
Destructor called automatically. */

}

The type size_t is a defined type capable of containing the largest single piece of memory that can be allocated. (size_t is essentially an unsigned integer.) The parameter size will contain the number of bytes needed to hold the object being allocated. This is the amount of memory that your version of new must allocate. The overloaded new function must return a pointer to the memory that it allocates, or throw a bad_alloc exception if an allocation error occurs.
The delete function receives a pointer to the region of memory to be freed. It then releases the previously allocated memory back to the system. When an object is deleted, its destructor is automatically called.
The new and delete operators may be overloaded globally so that all uses of these operators call your custom versions.


#include <iostream>
#include <cstdlib>
#include <new>
using namespace std;

class
loc {
int
longitude, latitude;
public
:
loc() {}
loc(int lg, int lt) {
longitude = lg;
latitude = lt;
}


void
show() {
cout << longitude << " ";
cout << latitude << "\n";
}


void
*operator new(size_t size);
void
operator delete(void *p);
};


// new overloaded relative to loc.
void *loc::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 loc.
void loc::operator delete(void *p)
{

cout << "In overloaded delete.\n";
free(p);
}


int
main()
{

loc *p1, *p2;

try
{
p1 = new loc (10, 20);
}
catch (bad_alloc xa) {
cout << "Allocation error for p1.\n";
return
1;
}


try
{
p2 = new loc (-10, -20);
}
catch (bad_alloc xa) {
cout << "Allocation error for p2.\n";
return
1;;
}


p1->show();
p2->show();

delete
p1;
delete
p2;

return
0;
}


Output from this program is shown here.

In overloaded new.
In overloaded new.
10 20

-
10 -20
In overloaded delete.
In overloaded delete.

When new and delete are for a specific class, the use of these operators on any other type of data causes the original new or delete to be employed. The overloaded operators are only applied to the types for which they are defined. This means that if you add this line to the main( ), the default new will be executed:

sample code ref: C++ complete reference (4rth edition)

No comments: