9

I was reading a question on SO and in one of the answers, it has been mentioned as:

If no unambiguous matching deallocation function can be found, propagating the exception does not cause the object’s memory to be freed.

So, if I just overload my new operator and not the delete operator, would any default delete operator be created and called; or, do I have to also write the delete operator explicitly.

Azeem
  • 7,659
  • 4
  • 22
  • 36
pasha
  • 1,987
  • 18
  • 32

1 Answers1

8

What this means is that if you overload operator new with extra arguments, and not the corresponding delete with extra arguments, if an exception occurs in a constructor, no delete operator will be called. On the other hand, if you're overloading the basic new (with no extra arguments), and an exception occurs, delete with no extra argument will be called, and that will be the default operator delete if you have not overloaded it.

Chris Dodd
  • 111,130
  • 12
  • 123
  • 212
  • Just to be explicit, so if I invoke new with some arguments, the delete taking same arguments will be invoked, correct? – pasha Sep 04 '18 at 05:43
  • if (one of) the constructors invoked after the `operator new` function returns throws an exception, yes. – Chris Dodd Sep 04 '18 at 05:48