wrapping new

J

john smith

I am trying to wrap new but am having some problems.

What I want to do is provide my own implementation of "new" Globally not by
class, but within my implementation of new call the regular global new.

So here is the picture.


void* operator new( ... ) // this is my implementation of new
{

// do something

void* operator new(...) // this is the regular new


}


Then when I call new in my program I want my implementation of "new" to be
called which would then call the regular "new".

I have played some games with namespaces but that did not work.

So, I am stuck. Any advice is greatly appreciated.
 
I

Ian

john said:
I am trying to wrap new but am having some problems.

What I want to do is provide my own implementation of "new" Globally not by
class, but within my implementation of new call the regular global new.

So here is the picture.


void* operator new( ... ) // this is my implementation of new
{

// do something

void* operator new(...) // this is the regular new


}


Then when I call new in my program I want my implementation of "new" to be
called which would then call the regular "new".

I have played some games with namespaces but that did not work.

So, I am stuck. Any advice is greatly appreciated.

I don't think a user defined new can coexist with the original. You are
replacing the symbol at link time.

Ian
 
J

John Harrison

john smith said:
I am trying to wrap new but am having some problems.

What I want to do is provide my own implementation of "new" Globally not by
class, but within my implementation of new call the regular global new.
[snip]

So, I am stuck. Any advice is greatly appreciated.

You are stuck because the new new is a replacement for the old new not a
wrapper.

Is there any reason you cannot call malloc from inside your new new (and
call free from inside a new delete)?

john
 
S

Scott Simontis

john smith said:
I am trying to wrap new but am having some problems.

What I want to do is provide my own implementation of "new" Globally not by
class, but within my implementation of new call the regular global new.

So here is the picture.


void* operator new( ... ) // this is my implementation of new
{

// do something

void* operator new(...) // this is the regular new


}


Then when I call new in my program I want my implementation of "new" to be
called which would then call the regular "new".

I have played some games with namespaces but that did not work.

So, I am stuck. Any advice is greatly appreciated.

Bruce Eckel's Thinking in C++: Volume 2, edition 2 had some stuff on
this. I personally don't like this book though. Volume 1 was great,
but I just didn't like Volume 2. I found it very difficult and boring,
but I know it does have some information on this.
 

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
474,175
Messages
2,570,942
Members
47,489
Latest member
BrigidaD91

Latest Threads

Top