I would like to override global operator new and delete to add some memory reporting functionality but continue to use the original operator new and delete to allocate and free memory. I'm having a hard time figuring out how. Does anyone know how to do this?
Replace but delegate to global operator new and delete
Collapse
X
-
-
All you have to do is write an operator new() and an operator delete() function.
The C++ new call operator new() to get the allocation and then goes on to call the constructor.
The C++ delete calls operator delete() after it has called the destructor.
Any C++ textbook shgould have this in it. -
Thanks for the reply. I was able to install the new global operators. The piece that I can't figure out is how to call the original operator delete from my code.
// In my main program class
// Installation of memory management
void* operator new (size_t bytes)
{
// Perform some logging
// Then call the original operator new
// So I don't need to re-invent the wheel for actually
// allocating memory
}
void operator delete(void* memoryPointer) throw()
{
// Perform some logging
// Then call the original operator delete
// So I don't need to re-invent the wheel for actually
// de-allocating memory
}Comment
-
Your function is operator new. The C++ function is std::operator new.
BTW: All of your functions should be in a namespace to avoid just this sort of difficulty.Comment
-
std:: Didn't even think of it. Excellent. Thanks for the help. Yes, I've been using namespaces for most of my classes and functions. I didn't think of putting the operator new and delete in a namespace since the goal is to replace the functionality of new and delete globally. You would still recommend putting them in a namespace though?Comment
-
oops. A typical screw-up. Sorry.
This works like overloading << for a class of your own. That is, overload rules require at least one argument of the overload to be a user-defined type. Plus, inside the overload code you will use the << of the built-in types. So your Date::operator< < really uses the << of the unsigned int to output the month , day and year.
In that spirit, your operator new (in your namespace is really an allocator for your class. Like this:
Here there is a private heap called buffer allocated in the MyStuff namespace using the operator new for unsigned char.Code:namespace MyStuff { unsigned char* buffer = new unsigned char[10000]; class MyClass { public: void* operator new[](size_t len, void* buffer) { MyClass* array = (MyClass*) new (buffer)unsigned char[len * sizeof(MyClass)]; for (size_t i = 0; i < len; ++i) { array[i].MyClass::MyClass(); } return array; } MyClass() { } }; } int main() { MyStuff::MyClass* ptr = new (MyStuff::buffer)MyStuff::MyClass[3]; }
In MyClass there is an overload of operator new[] to allocate arrays of MyClass objects where the array is located in a buffer of unsigned char. How you manage buffer has been omitted (this is the hard part).
When you dynamically create a MyClass array it is the MyClass::operat or new that is called. The overload grabs memory using the placement new version of the operator new for built-in types. In this case the array is allocated from a buffer of usigned char. No MyClass constructors are called so your MyClass operator new has to call the MyClass constructors for the array elements.
This set up assumes you have a MyClass::delete to clean up the MyClass::operat or new.
You may need several MyClass::operat or new functions: a) for one object, b) for an array of objects.
Finally, if you are using C++ Standard Library objects, you need to research allocators instead of just overloading operator new. They have to be written a specific way.Comment
-
No need to apologize. You've been extremely helpful and I greatly appreciate it. It just looks like there isn't an easy way to override the global operator new to add additional functionality without completely replacing it. I did run across some possibilities using macros but I'm leery of using a set of complicated macros. I think my best bet is to replace the operator completely and write my own functionality to allocate (and deallocate) memory.
Thanks again!Comment
-
Yes, the C++ designers stuck in the requirements that a) one argument of the overload must be a user defined type and b) you could not invent new operators and c) some operators can't be overloaded (like ::).
This is to prevent you from altering how ints are added by using your own operator+ for ints, etc.Comment
Comment