Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I call the original "operator new" if I have overloaded it?

Suppose I need to overload global ::operator new() for storing extra data with each allocated object. So basically it would work this way:

  • for each call to global ::operator new() it will take the object size passed and add the size of extra data
  • it will allocate a memory block of size deduced at previous step
  • it will offset the pointer to the part of the block not occupied with extra data and return that offset value to the caller

::operator delete() will do the same in reverse - shift the pointer, access extra data, deallocate memory.

Now the question is how do I allocate memory? Of course I can call malloc() or some platform-specific function (that's how it is usually done). But normally when I need to allocate raw memory in C++ I call ::operator new(). Can I call the original ::operator new() to do the memory allocation from inside my overloaded global ::operator new()?

like image 679
sharptooth Avatar asked Nov 09 '10 13:11

sharptooth


People also ask

Can I create new operators using operator overloading?

New operators can not be created. 2) Arity of the operators cannot be changed. 3) Precedence and associativity of the operators cannot be changed. 4) Overloaded operators cannot have default arguments except the function call operator () which can have default arguments.

What should the first parameter for an overloaded new operator?

The first parameter is always an ostream object (we've mostly used cout, so far). Because of this, it cannot be defined as a member function (it would have to be a member of the ostream class, which we cannot change). The << and >> operators should always be defined as outside functions (usually friend functions).

When you overload an operator you can change the operators?

That is, each class's member functions have free access to the other's private members. When you overload an operator, you can change the operator's original meaning to something entirely different. You just studied 72 terms!

Which operator is used for overloaded?

The = and & C++ operators are overloaded by default. For example, you can copy the objects of the same Class directly using the = operator.


1 Answers

You can't access them because it isn't really overloading, it's replacement. When you define your own ::operator new, the old one goes away. That's pretty much that.

Essentially, you need to call malloc from a custom ::operator new. Not only that, but also follow the directions in 18.4.1.1/4 to properly handle errors:

Default behavior:

— Executes a loop: Within the loop, the function first attempts to allocate the requested storage. Whether the attempt involves a call to the Standard C library function malloc is unspecified.

— Returns a pointer to the allocated storage if the attempt is successful. Otherwise, if the last argument to set_new_handler() was a null pointer, throw bad_alloc.

— Otherwise, the function calls the current new_handler (18.4.2.2). If the called function returns, the loop repeats.

— The loop terminates when an attempt to allocate the requested storage is successful or when a called new_handler function does not return.

like image 85
Potatoswatter Avatar answered Sep 23 '22 11:09

Potatoswatter