473,406 Members | 2,387 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Replace but delegate to global operator new and delete

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?
Dec 25 '09 #1
8 5245
weaknessforcats
9,208 Expert Mod 8TB
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.
Dec 27 '09 #2
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
}
Dec 28 '09 #3
weaknessforcats
9,208 Expert Mod 8TB
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.
Dec 28 '09 #4
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?
Dec 28 '09 #5
So after taking a second look at this, I'm not finding operator new or delete in the std namespace. I've included <new>. It actually looks to me like the original global operator new is not in a namespace. Is there something I'm missing?
Dec 28 '09 #6
weaknessforcats
9,208 Expert Mod 8TB
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:

Expand|Select|Wrap|Line Numbers
  1. namespace MyStuff
  2. {
  3.     unsigned char* buffer   = new unsigned char[10000];
  4.     class MyClass
  5.     {
  6.     public:
  7.           void* operator new[](size_t len, void* buffer)
  8.           {
  9.                MyClass* array = (MyClass*) new (buffer)unsigned char[len * sizeof(MyClass)];
  10.                 for (size_t i = 0; i < len; ++i)
  11.                 {
  12.                     array[i].MyClass::MyClass();
  13.                 }
  14.  
  15.                return array;
  16.  
  17.           }
  18.           MyClass()
  19.           {
  20.  
  21.           }
  22.  
  23.  
  24.  
  25.     };
  26.  
  27.  
  28. }
  29.  
  30. int main()
  31. {
  32.     MyStuff::MyClass* ptr = new (MyStuff::buffer)MyStuff::MyClass[3];
  33. }
Here there is a private heap called buffer allocated in the MyStuff namespace using the operator new for unsigned char.

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::operator 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::operator new.

You may need several MyClass::operator 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.
Dec 29 '09 #7
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!
Dec 29 '09 #8
weaknessforcats
9,208 Expert Mod 8TB
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.
Dec 30 '09 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: foo | last post by:
I'm creating a debug class called debug_mem_allocation for the purpose of finding memory leaks. I used macro's to replace the new and delete operators. My problem is with trying to replace the...
1
by: Dodo | last post by:
I have overloaded the global new/delete operators with something like this (simplified): void *operator new(size_t size) { ...allocation code... } void operator delete(void * p) {
1
by: Nimmi Srivastav | last post by:
There's a rather nondescript book called "Using Borland C++" by Lee and Mark Atkinson (Que Corporation) which presents an excellent discussion of overloaded new and delete operators. In fact there...
7
by: Emanuel Ziegler | last post by:
Hello, I want to do some mathematics with functions. In my case the function classes are very complex, but this simple example has the same problems. To allow calculations that begin with a...
0
by: Tim Milstead | last post by:
(CODE AT END OF POST) I have got some code for finding memory leaks that works well with: new() and needs to be extended to work with new
1
by: Zachary Turner | last post by:
I'm having trouble overloading the global operator new and delete. I want to do this for debugging purposes to trace allocations and deallocations. What I have is #ifdef _DEBUG //...
8
by: UDBDBA | last post by:
All: Anyone had bad experience with doing DECLARE DGTT "WITH REPLACE" option? The suggested workaround was to do "DELETE FROM DGTT" sql statement. We are seeing DGTT performance issue when...
15
by: Sharon | last post by:
I’m trying to build a generic Publisher-Subscriber that will work over the net, so I’m using the Remoting. I wish that the subscriber user will be notify about the messages sent by the...
1
by: dilabox | last post by:
Hello, I have overloaded the global new, delete, new and delete operators inside a "static library". The library uses private memory allocation routines that must not be accessible from other...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.