Skip to main content
GameDev.net gamedev.net
🔒 Locked

Overloading Global operator new in C++

Started by SecondBest Sep 17, 2007 at 7:40 AM 6 replies 1.6k views
Original Post
SecondBest
SecondBest
Hello All I overloaded the global operator new to act in a certain manner when provided with additional arguments.

class key{};

void* operator new(size_t sz, key&)
{
    //do some stuff
    return ::operator new(sz);
};
This works fine, except for classes for which operator new is already overloaded.

class test
{
public:
	void* operator new(size_t sz)
	{
		cout << "using custom new"<<endl;
		return ::operator new(sz);
	};
};

int main(int argc, char* argv[])
{
	k k1;
	int*  p_int = new(k1) int;\\ok
        test* p_test1 = new test;\\ok
        test* p_test2 = new(k1) test;\\error
        test* p_test3 = ::new(k1) test;\\ok, but doesn't use test's operator new
	return 0;
}
I am compiling using VC++ 6 and the error I get is: error C2660: 'new' : function does not take 2 parameters Is there a way to support creating my custom new without affecting user class defined new? thanks
ToohrVyk
ToohrVyk
Quote:
\\ok, but doesn't use test's operator new


Which operator new in test do you want to use?

SecondBest
SecondBest
I want my global operator new to do it's stuff and then call whatever is supplied by the system (i.e. default new, other overloaded global new operators, class specific new operator).
ToohrVyk
ToohrVyk
I doubt there is any simple solution to this. Your operator new has no information about what type it's being called on, so any "forwarding" also cannot depend on that type.

The simplest thing you can do is implement intrusively the operator new(size_t, key&) in any classes you wish to use it on, and have it forward the call to the class' operator new.
Vorpy
Vorpy
If the best solution is to implement the function intrusively, it might be easiest to do this by inheriting from a template class that uses the curiously recurring template pattern unless there's some reason I'm not seeing for why this wouldn't work.
Sneftel
Sneftel
The purpose of a placement-new operator is to allocate memory for an object. The purpose of a class-specific new operator is to allocate memory for an object. If you were to invoke both of those, that would equate to allocating memory twice. What are you trying to accomplish.. tracking allocations?
KreK
KreK
Just a small correction. Placement new doesn't allocate any memory but constructs an object on a previously allocated memory or even on a stack.
Sneftel
Sneftel
Quote:
Original post by KreK
Just a small correction. Placement new doesn't allocate any memory but constructs an object on a previously allocated memory or even on a stack.

You're thinking specifically of the placement-new operator which takes a void*. Semantically, the general meaning of new, including placement-new, is the allocation of memory to a resource (note that the term "allocation" doesn't necessarily imply malloc).

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.