Sign in to follow this  
Silly_con

overloading global new operator

Recommended Posts


typedef void* (*global_new_ptr)(size_t);

global_new_ptr global_new = ::operator new;

void* operator new(size_t s)
{
// do some book keeping
// allocate memory
return (*global_new)(s);
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Silly_con
still recursive


Well of course, thats not overloading that is just redefining it (well trying to and going into an infinite recusion) you wont something like this:


#include <new>
#include <iostream>

struct foo_t {};

extern const foo_t foo; //no need to define it.

inline void* operator new(size_t n, const foo_t&) throw() {
std::cout << "MY NEW\n";
return ::operator new(n, std::nothrow);
}

inline void operator delete(void* ptr, const foo_t&) throw() {
std::cout << "MY DELETE\n";
::operator delete(ptr, std::nothrow);
}

int main() {


int* int_ptr = new(foo) int(30);

std::cout << *int_ptr << std::endl;

operator delete(int_ptr, foo);
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this