Entering restrictions when reloading new ones and deleting

Is it possible to set some restrictions when overloading the new and delete operators? My overloaded new is linked to another file in my test program.

Scenario:

if(condition is satisfied) call overloaded new else call the actual new defined in new.h 
+2
source share
4 answers

There are three ways to provide a new operator.

  • replace one or more of the four default default operators with new,

  • providing overload for the default operator new (thus, with an additional parameter, they can be called with the new layout syntax)

  • providing operators with new members of the class, they will be called only for this class and their descendant.

In the last two cases, one of the most well-known operators can be called new with the syntax:

 ptr = ::operator new(sz); ptr = ::operator new[](sz); ptr = ::operator new(sz, std::nothrow); ptr = ::operator new[](sz, std::nothrow); 

but if you replace them, your replacement will be called. You cannot call the new default operators that you replaced (well, maybe you can by playing specific tricks with specific specific implementations, but not included in the language).

About replacing the new operator:

  • you should replace two new operators and link the two operators together (or one of the delete operators could be easily called using an unexpected pointer)
  • you must replace the two new [] statements and the corresponding two delete [] statements together (for the same reason)
  • pay attention to what's possible with new handlers, some libraries play with this.
0
source

Always use the overloaded new / delete and check your state inside its implementation.

+3
source

After replacing the standard ::operator new() you can no longer use it - it disappeared forever. See this question .

If you want to have the effect of the original ::operator new() , you have to redefine it, which is not very difficult.

+1
source

You can easily check in your overloaded new operator. Be sure to implement all the tastes of the new operator (as AProgrammer already pointed out).

Calling the original / default new is not possible, but it is not difficult to complete. In the end, the new only highlights the memory that it is. Therefore, instead of calling the original / default, you can also call malloc, HeapAlloc, or any memory allocation procedure found on your system. Be sure to call the appropriate memory deletion method (free, HeapFree, ...) in your delete implementation.

You did not say what condition you are going to check in your implementation? If this is a β€œstatic” condition (I mean: always giving the same result at runtime of your application), then the same condition should be added to your removal implementation.

If the condition depends on the situation and changes when the application is launched, you should foresee a method in which you can find out which implementation of deletion is used in your delete function. One trick does the following:

In your implementation of the new:

  • allocate 8 bytes more than requested (this should be 8 bytes to keep alignment correct)
  • enter the first 8 bytes with identification so that you can find out what basic memory allocation function you used
  • add 8 bytes to the highlighted pointer and return this

In your implementation of delete:

  • subtract 8 bytes of the pointer provided to you
  • check the identification found in this place (see new) to see what type of base implementation-deletion you should name
0
source

Source: https://habr.com/ru/post/1389606/


All Articles