How does C++ free the memory when a constructor throws an exception and a custom new is used

前端 未结 5 1089
春和景丽
春和景丽 2021-02-19 16:07

I see the following constructs:

  • new X will free the memory if X constructor throws.

  • operator new() can be

5条回答
  •  滥情空心
    2021-02-19 16:25

    'placement new' is not an overloaded version of new, but one of the variants of operator new , and also one that cannot be overloaded.

    See can see the list of new operators here along with a description on how overloading them works.

    If a constructor throws an exception when using placement new the compiler knows what new operator was used and call placement delete.

提交回复
热议问题