Re: Overloading "new" and "delete" operator

M

Min

Finally, it works now, Thanks.
I was at the local and university librarys searching for overloading
operator new. All books I searched for talk only about overloading other
operators such as >>, <<. ==, = etc. but not "new" "new []", "delete",
"delete []"; I can't remember each titles, but I am sure none talks about
the detail I was looking for. Even Effective C++ by Scott Meyers, the guru
won't talk about "new [ ]" operator (only new)

PS : Finishing "Writing Solid Code" by Steve Maguire (in C), I thought I
should try some of the interesting techniques in my projects which are
mostly in C++. Thats why I needed some help.

Thanks again.

Victor Bazarov said:
Min said:
I am really stuck and can't find any good reference,

What book do you have? I'd like to know to recommend others
not to get it since it's apparently of little help to those
who need help.
I hope some one here
can help me.

Someone sure can.
What I am trying to accomplish is writing my own "new" and "delete" operator
to keep a memory log in debug version for memory leak verification purpose.

Example:
int * pInt = new int; // will call default "new"
Foo* pFoo = new Foo(); // will call the overloaded new.
Foo* pFooArry = new Foo[100]; // will call the overloaded new.

Also, help me clarify the difference between "Overloading Operator New" and
"Overloading New Operator".

The function you overload (as you already have done) is called
"operator new". So, when you define your own in your class, you
"overload operator new". "overloading new operator" is something
I've not heard.
There is my attempt, but does not work, as someone has already pointed out
that I also need to overload "[ ]" operator. But I don't get it, because
all tutorials on overloading "[ ]" talk about accessing a data within the
object, rather than an array of object.

"someone" has pointed out that you need to overload operator new[],
not operator[].
Also, how would it work if we are
also talking about inheritance.
Thanks

//***************************** CODE *************************
class Foo
{
public:
char name[20];
Foo() { /*** Do Something ***/ }
Foo( char* str) { /*** Do Something ***/ }

static void * operator new(unsigned int size)

Drop "static" here. It will be static because the language
dictates it, you don't have to declare it 'static'.
{
//*********************
// Code from Effective C++
//*********************
void* memory = null;
if ( size == 0 ) { size = 1; }
try
{
memory = ::eek:perator new(size);
}
catch (std::bad_alloc&)
{
throw;
}
return memory;
}

So, you need to add

void * operator new[](size_t)
{
...
}

And you probably also want to add

void operator delete(void *);
void operator delete[](void *);
};

void main()

'main' returns 'int'.
{
Foo * ptrFoo1 = new Foo(); // Uses Overloaded New
Foo * ptrFoo2 = new Foo("What's up"); // Uses Overloaded New
Foo *ptrFoo3 = new Foo[10]; // Uses DEFAULT New.
}
//***************** END OF CODE *************************
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top