New/Delete Builtins (Using the GNU Compiler Collection (GCC)) (original) (raw)


7.11 Built-in functions for C++ allocations and deallocations

GNU C++ provides builtins that are equivalent to calling::operator new or ::operator delete with the same arguments. It is an error if the selected ::operator new or::operator delete overload is not a replaceable global operator. For optimization purposes, calls to pairs of these builtins can be omitted if access to the allocation is optimized out, or could be replaced with an implementation-provided buffer on the stack, or multiple allocation calls can be merged into a single allocation. In C++ such optimizations are normally allowed just for calls to such replaceable global operators from new and deleteexpressions.

void foo () { int *a = new int; delete a; // This pair of allocation/deallocation operators can be omitted // or replaced with int _temp; int *a = &_temp; etc. void *b = ::operator new (32); ::operator delete (b); // This one cannnot. void *c = __builtin_operator_new (32); __builtin_operator_delete (c); // This one can. }

These built-ins are only available in C++.

Built-in Function: void * __builtin_operator_new (std::size_t size, ...)

This is the built-in form of operator new. It accepts the same argument forms as a “usual allocation function”, as described in the C++ standard.

Built-in Function: void __builtin_operator_delete (void * ptr, ...)

This is the built-in form of operator delete. It accepts the same argument forms as a “usual deallocation function”, as described in the C++ standard.