S
sukhpal
Can any please let me know the difference between new and operator
new. i have written follwoing example in which i have overloaded new
operator.
When i am using "void *p = operator new(1);" it doesn't call the new
overloaded operator. So waht is operator new.When we use it?
#include <iostream>
#include <cstdlib>
#include <new>
using namespace std;
class MyClass {
int x, y;
public:
MyClass() {
x = y = 0;
}
MyClass(int lg, int lt) {
std::cout << "in a constructor";
x = lg;
y = lt;
}
void show() {
cout << x << " ";
cout << y << endl;
}
void *operator new(size_t size);
void operator delete(void *p);
void *operator new[](size_t size);
void operator delete[](void *p);
};
// overloaded new operator
void *MyClass:perator new(size_t size)
{
std::cout << "in a operator new";
void *p;
cout << "In overloaded new.\n";
p = malloc(size);
if(!p) {
bad_alloc ba;
throw ba;
}
return p;
}
// delete operator overloaded
void MyClass:perator delete(void *p)
{
cout << "In overloaded delete.\n";
free(p);
}
// new operator overloaded for arrays.
void *MyClass:perator new[](size_t size)
{
void *p;
cout << "Using overload new[].\n";
p = malloc(size);
if( !p ) {
bad_alloc ba;
throw ba;
}
return p;
}
// delete operator overloaded for arrays.
void MyClass:perator delete[](void *p)
{
cout << "Freeing array using overloaded delete[]\n";
free(p);
}
int main()
{
MyClass *objectPointer1, *objectPointer2;
int i;
void *p = operator new(1);
try {
objectPointer1 = new MyClass (10, 20);
} catch (bad_alloc xa) {
cout << "Allocation error for objectPointer1.\n";
return 1;;
}
try {
objectPointer2 = new MyClass [10]; // allocate an
array
} catch (bad_alloc xa) {
cout << "Allocation error for objectPointer2.\n";
return 1;;
}
objectPointer1->show();
for( i = 0; i < 10; i++)
objectPointer2.show();
delete objectPointer1; // free an object
delete [] objectPointer2; // free an array
int ruk;
std::cin >> ruk;
return 0;
}
new. i have written follwoing example in which i have overloaded new
operator.
When i am using "void *p = operator new(1);" it doesn't call the new
overloaded operator. So waht is operator new.When we use it?
#include <iostream>
#include <cstdlib>
#include <new>
using namespace std;
class MyClass {
int x, y;
public:
MyClass() {
x = y = 0;
}
MyClass(int lg, int lt) {
std::cout << "in a constructor";
x = lg;
y = lt;
}
void show() {
cout << x << " ";
cout << y << endl;
}
void *operator new(size_t size);
void operator delete(void *p);
void *operator new[](size_t size);
void operator delete[](void *p);
};
// overloaded new operator
void *MyClass:perator new(size_t size)
{
std::cout << "in a operator new";
void *p;
cout << "In overloaded new.\n";
p = malloc(size);
if(!p) {
bad_alloc ba;
throw ba;
}
return p;
}
// delete operator overloaded
void MyClass:perator delete(void *p)
{
cout << "In overloaded delete.\n";
free(p);
}
// new operator overloaded for arrays.
void *MyClass:perator new[](size_t size)
{
void *p;
cout << "Using overload new[].\n";
p = malloc(size);
if( !p ) {
bad_alloc ba;
throw ba;
}
return p;
}
// delete operator overloaded for arrays.
void MyClass:perator delete[](void *p)
{
cout << "Freeing array using overloaded delete[]\n";
free(p);
}
int main()
{
MyClass *objectPointer1, *objectPointer2;
int i;
void *p = operator new(1);
try {
objectPointer1 = new MyClass (10, 20);
} catch (bad_alloc xa) {
cout << "Allocation error for objectPointer1.\n";
return 1;;
}
try {
objectPointer2 = new MyClass [10]; // allocate an
array
} catch (bad_alloc xa) {
cout << "Allocation error for objectPointer2.\n";
return 1;;
}
objectPointer1->show();
for( i = 0; i < 10; i++)
objectPointer2.show();
delete objectPointer1; // free an object
delete [] objectPointer2; // free an array
int ruk;
std::cin >> ruk;
return 0;
}