1

Possible Duplicate:
Any reason to overload global new and delete?

In c++ you can overload new/ delete operators, are there any benefits in doing so? Since right after calling operator new its result value is sent to the constructor and right before calling operator delete, the destructor is called.

Community
  • 1
  • 1
Ali1S232
  • 3,373
  • 2
  • 27
  • 46

4 Answers4

6

The point of overloading is to control the memory allocation. In some cases you want to use your own allocators instead of the standard ones (for example, when you want allocations to be from static pools and not directly from the heap).

littleadv
  • 20,100
  • 2
  • 36
  • 50
1

It's useful if you want to write your own allocator, then you can overload new and delete to make the use of that allocator more natural.

Etienne de Martel
  • 34,692
  • 8
  • 91
  • 111
1

Say you have code for a mobile device. You can overload new in order to manage memory your way, maybe more efficiently than the default behavior.

George Kastrinis
  • 4,924
  • 4
  • 29
  • 46
0

Overloading new and delete can be useful if you want to write your own memory usage tracker. Also useful if you want to simulate low memory conditions.

sean e
  • 11,792
  • 3
  • 44
  • 56