custom function called a with the C++ runtime; if it does, then this function is called whenever encounters an error. The may attempt to make more memory available, or terminate the program if it can't. If no is installed, instead throws an exception of type . Thus, the program does not need to check the value of the returned pointer, as is the habit in C; if no exception was thrown, the allocation succeeded. The third method of error handling is provided by the variant form , which specifies that no exception should be thrown; instead, a null pointer is returned to signal an allocation error. Overloading The operator can be overloaded so that specific types (classes) use custom memory allocation algorithms for their instances. For example, the following is a variant of the singleton pattern where the first call allocates an instance and all subsequent calls return this same instance: #include <cstdlib> #include <cstddef> class Singleton { public: static void* operator new(std::size_t size) { if (!instance) { instance = std::malloc(size); } refcount++; return instance; } static void operator delete(void*) noexcept { if (--refcount == 0) { std::free(instance); instance = nullptr; } } private: static void* instance = nullptr; static std::size_t refcount = 0; }; This feature was available from early on in C++'s history, although the specific overloading mechanism changed. It was added to the language because object-oriented C++ programs tended to allocate many small objects with , which internally used the C allocator (see ); that, however, was optimized for the fewer and larger allocations performed by typical C programs. Stroustrup reported that in early applications, the C function was "the most common performance bottleneck in real systems", with programs spending up to 50% of their time in this function. . Relation to malloc and free Since standard C++ subsumes the C standard library, the C dynamic memory allocation routines , , and are also available to C++ programmers. The use of these routines is discouraged for most uses, since they do not perform object initialization and destruction. and were, in fact, introduced in the first version of C++ (then called "C with Classes") to avoid the necessity of manual object initialization. In contrast to the C routines, which allow growing or shrinking an allocated array with , it is not possible to change the size of a memory buffer allocated by . The C++ standard library instead