By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,696 Members | 1,512 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,696 IT Pros & Developers. It's quick & easy.

Overloading new and delete operators

P: n/a
There's a rather nondescript book called "Using Borland C++" by Lee
and Mark Atkinson (Que Corporation) which presents an excellent
discussion of overloaded new and delete operators.

I am presenting below a summary of what I have gathered. I would
appreciate if someone could point out to something that is specific to
Borland C++ and is not supported by the ANSI standard. I am also
concerned that some of the information may be outdated since the book
is quite old (1991 edition).
1) We cannot use the array size declarator with a class-specific
user-defined new function. Arrays of objects are always allocated
with the global new.

2) Overloaded new and delete operators should be provided in pairs.

3) Class-specific new and delete functions are always static member
functions, even though they are not explicitly declared with the
static storage class specifier.

4) A class can have many overloaded new functions ---- the correct
function is selected through best-fit signature matching, as for any
other overloaded function.

5) The first argument of each overloaded new operator is a size_t
object size argument. However, this argument is implicit and is not
provided when the overloaded new operator is invoked.

6) A class can have only one overloaded delete operator.

7) The overloaded delete operator is invoked after the last line of
the destructor has been executed.
// ~~~~~~~ Code snippet begin ~~~~~~~
#include <iostream.h>

/////////////////////////////////////////////////////////////////
class X
{
int a, b, c;

public:
~X() { cout << "Inside destructor of class X" << endl; }

// You are allowed to declare only one delete function
void operator delete( void* ptr);

// You can overload new however you want, as long as the first
// argument is always a size_t object-size argument
//
// !!!!! Important note: The size_t parameter is
// implicit. Do not write it directly into
// the function call. !!!!!
void* operator new(size_t size);
void* operator new(size_t size, void* ptr);
void* operator new(size_t size, int flag);
};
/////////////////////////////////////////////////////////////////
void* X::operator new(size_t size)
{
void* ptr;
cout << "Standard class new called.\r\n";
ptr = (void*) ::new unsigned char[size];
return ptr;
}
/////////////////////////////////////////////////////////////////
void* X::operator new(size_t size, void* ptr)
{
size = size;
cout << "Placement class new called.\r\n";
return ptr;
}

/////////////////////////////////////////////////////////////////
void* X::operator new(size_t size, int flag)
{
void* ptr;
cout << "Overloaded class new called. ";
cout << "Flag was " << flag << ".\r\n";
ptr = (void*) ::new unsigned char[size];
return ptr;
}
/////////////////////////////////////////////////////////////////
void X::operator delete( void* ptr)
{
cout << "Class delete called.\r\n";
free(ptr);
}
/////////////////////////////////////////////////////////////////
int main()
{
X* objl = new X;
// Remember the size_t parameter is implicit
// The standard class new is called
// void* X::operator new(size_t size)
delete objl;

X* obj2;
void* buf = (void*) ::new unsigned char[sizeof(X)];
obj2 = new(buf) X;
// Remember the size_t parameter is implicit
// The placement class new called
// void* X::operator new(size_t size, void* ptr)
free(buf);

X* obj3 = new(64) X;
// Remember the size_t parameter is implicit
// The overloaded class new called
// void* X::operator new(size_t size, int flag)
delete obj3;
}
// ~~~~~~~ Code snippet end ~~~~~~~

Thanks,
Nimmi
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

"Nimmi Srivastav" <ni*************@yahoo.com> wrote in message
news:8b*************************@posting.google.co m...
There's a rather nondescript book called "Using Borland C++" by Lee
and Mark Atkinson (Que Corporation) which presents an excellent
discussion of overloaded new and delete operators.

I am presenting below a summary of what I have gathered. I would
appreciate if someone could point out to something that is specific to
Borland C++ and is not supported by the ANSI standard. I am also
concerned that some of the information may be outdated since the book
is quite old (1991 edition).
1) We cannot use the array size declarator with a class-specific
user-defined new function. Arrays of objects are always allocated
with the global new.
ANSI supports overloading operator new[] and delete[] which are used for
allocating arrays. So the above is incorrect.

2) Overloaded new and delete operators should be provided in pairs.
I guess but placement delete complicates this. E.g.

class Y;
class X
{
public:
void* operator new(size_t bytes, Y*);
void operator delete(void* ptr);
void operator delete(void* ptr, Y*);
};

See, two deletes and only one new.

3) Class-specific new and delete functions are always static member
functions, even though they are not explicitly declared with the
static storage class specifier.
Correct.

4) A class can have many overloaded new functions ---- the correct
function is selected through best-fit signature matching, as for any
other overloaded function.
Correct AFAIK.Although this is usually called placement new.

5) The first argument of each overloaded new operator is a size_t
object size argument. However, this argument is implicit and is not
provided when the overloaded new operator is invoked.
It must be provided. How else would you know how much memory to allocate?
E.g.

class X
{
void operator new(size_t bytes);
};

class Y : public X
{
char big[1000];
};

Both new X and new Y will call X::operator new. But the amount of memory to
allocate is different.

6) A class can have only one overloaded delete operator.
Not correct. If you use placement new, then you should provide matching
placement deletes. These will be called of the ctor of your class throws,
IIRC.

7) The overloaded delete operator is invoked after the last line of
the destructor has been executed.


True enough.

I think you need a more up to date book.

John
Jul 22 '05 #2

P: n/a
"John Harrison" <jo*************@hotmail.com> wrote in message news:<bv************@ID-196037.news.uni-berlin.de>...
ANSI supports overloading operator new[] and delete[] which are used for
allocating arrays. So the above is incorrect.

Could you be kind enough to provide a code snippet for overloaded
new[] and delete[]? Can new[] and delete[] be used for instantiating
and deleting on the free store 'SINGLE ENTITIES' also, or do you need
separate methods for that? In other words, would you need four
overloaded operator methods ---- new, new[], delete & delete[]?

2) Overloaded new and delete operators should be provided in pairs.


I guess but placement delete complicates this. E.g.

class Y;
class X
{
public:
void* operator new(size_t bytes, Y*);
void operator delete(void* ptr);
void operator delete(void* ptr, Y*);
};

See, two deletes and only one new.


I am curious what you would typically do inside the body of
void operator delete(void* ptr, Y*). As always, a code snippet will
help

Not correct. If you use placement new, then you should provide matching
placement deletes. These will be called of the ctor of your class throws,
IIRC.


As mentioned above, working examples of "placement delete" would help.

Thanks and Best Regards,
Nimmi
Jul 22 '05 #3

P: n/a

"Nimmi Srivastav" <ni*************@yahoo.com> wrote in message
news:8b*************************@posting.google.co m...
"John Harrison" <jo*************@hotmail.com> wrote in message news:<bv************@ID-196037.news.uni-berlin.de>...
ANSI supports overloading operator new[] and delete[] which are used for
allocating arrays. So the above is incorrect.


Could you be kind enough to provide a code snippet for overloaded
new[] and delete[]? Can new[] and delete[] be used for instantiating
and deleting on the free store 'SINGLE ENTITIES' also, or do you need
separate methods for that? In other words, would you need four
overloaded operator methods ---- new, new[], delete & delete[]?


Exactly, four methods.

Code for new[] and delete[] is exactly the same as for new and delete except
that you add []. E.g.

void* X::operator[](size_t bytes)
{
...
}


2) Overloaded new and delete operators should be provided in pairs.
I guess but placement delete complicates this. E.g.

class Y;
class X
{
public:
void* operator new(size_t bytes, Y*);
void operator delete(void* ptr);
void operator delete(void* ptr, Y*);
};

See, two deletes and only one new.


I am curious what you would typically do inside the body of
void operator delete(void* ptr, Y*). As always, a code snippet will
help


I guess typoically Y would be some sort of memory pool from which you are
allocating objects. For some reason you might want several memory pools
instead of the one large one.

Placement delete is very specialised, it is only called when the ctor of an
object you have constructed with placement new throws. To be perfectly
honest I cannot remember the reasoning behind this rule, maybe you can work
it out.

Not correct. If you use placement new, then you should provide matching
placement deletes. These will be called of the ctor of your class throws, IIRC.

As mentioned above, working examples of "placement delete" would help.


This code outputs

normal delete
placement delete

#include <iostream>
using namespace std;

class Y;

class X
{
public:
X() {} // normal ctor
X(int) { throw ""; } // throwing ctor
void* operator new(size_t bytes, Y*)
{
return malloc(bytes);
}
void operator delete(void* ptr)
{
cout << "normal delete\n";
}
void operator delete(void* ptr, Y*)
{
cout << "placement delete\n";
}
};

int main()
{
try
{
Y* y = 0;
X* p = new (y) X();
delete p;
p = new (y) X(1);
delete p;
}
catch (...)
{
}
}
Thanks and Best Regards,
Nimmi


John
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.