Exception safety of new operator

390 views Asked by At

This is implementation of new operator in libstdc++:

_GLIBCXX_WEAK_DEFINITION void *
operator new (std::size_t sz) _GLIBCXX_THROW (std::bad_alloc)
{
  void *p;

  /* malloc (0) is unpredictable; avoid it.  */
  if (__builtin_expect (sz == 0, false))
    sz = 1;

  while ((p = malloc (sz)) == 0)
    {
      new_handler handler = std::get_new_handler ();
      if (! handler)
        _GLIBCXX_THROW_OR_ABORT(bad_alloc());
      handler ();
    }

  return p;
}

Who guarantees that exception in constructor will free the allocated memory?

Upd: thanks to the commentators - actually I meant exception safety of new expression.

1

There are 1 answers

4
PiotrNycz On BEST ANSWER

You are mixing in your question "new expression" and "operator new".

New expression is like this: A* a = new A(); C++ language defines, that this expression is evaluated to something like this (over-simplified pseudo-code):

void* __a = operator new(sizeof(A));
try {
   A::A(this = static_cast<A*>(__a));
} catch (...) {  operator delete (__a); throw; }

As you can see - memory is dealocated if exception happens.

More detailed explanation:

If initialization terminates by throwing an exception (e.g. from the constructor), if new-expression allocated any storage, it calls the appropriate deallocation function: operator delete for non-array type, operator delete[] for array type.


For real generated code by compiler - refer here. You might notice call to operator delete - even if source code does not contain it:

struct A { A(); };
A::A() { throw 13; }

int main()
{
    auto a = new A();
}

Assembler:

 call 401040 <operator new(unsigned long)@plt>
...
 call 401172 <A::A()>
...
 call 401050 <operator delete(void*, unsigned long)@plt>
...
 call 401080 <_Unwind_Resume@plt>