Apache C++ Standard Library Reference Guide

## operator new

Library: Language support

    Function

- Local Index
- Summary
- Synopsis
- Description
- Global Functions
- Global Operators
- See Also
- Standards Conformance

### Local Index

#### Non-Members

new() | set_new_handler() |

### Summary

Storage allocation functions implicitly called by the corresponding new expressions to allocate storage suitably aligned to construct any object of a given size

### Synopsis

    #include <new>

    namespace std {
        class bad_alloc;
        struct nothrow_t {};
        extern const nothrow_t nothrow;
        typedef void (*new_handler)();
        new_handler set_new_handler new_handler) throw();
    }

    void* operator new(std::size_t) throw (std::bad_alloc);
    void* operator new(std::size_t, const std::nothrow_t&)
            throw();
    void* operator new[](std::size_t) throw(std::bad_alloc);
    void* operator new[](std::size_t, const std::nothrow_t&)
            throw();
    void* operator new(std::size_t, void*) throw();
    void* operator new[](std::size_t, void*) throw();

### Description

The library provides definitions for six overloads of the global operator `new`. The functions are implicitly called as the first step during the evaluation of the corresponding `new` expression to allocate storage suitably aligned to construct one or more objects (for the array versions of the operator) of any type. The functions indicate failure by either throwing an exception object of type bad_alloc or by returning the null pointer. Replacements for the replaceable forms of the functions should always paired with the replacements for the corresponding overload of operator `delete`.

### Global Functions

    new_handler
    set_new_handler(new_handler handler) throw();

Establishes handler as the current new_handler. The first time `set_new_handler()` is called, it returns the null pointer. Subsequent calls to `set_new_handler()` return the previous value of new_handler.

### Global Operators

    void* operator new(std::size_t n) throw(std::bad_alloc);

Allocation function implicitly called by a `new` expression to allocate `n` bytes of storage. The function attempts to allocate storage in a loop: if the allocation is successful, a pointer to the allocated memory is returned. Otherwise, if the last argument to `std::set_new_handler()` was not a null pointer, the function calls the current new_handler. Otherwise it throws an exception object of type bad_alloc. The loop terminates if the allocation is successful or the current new_handler doesn't return. A C++ program may replace this function.

    void* operator new(std::size_t n, const std::nothrow_t&) throw();

Allocation function similar to the one above, except that it indicates failure by returning a null pointer. A C++ program may replace this function.

    void* operator new[](std::size_t n) throw(std::bad_alloc);

Allocation function implicitly called by the array form of a `new` expression to allocate `n` bytes of storage. The function returns `::operator new (n)`. A C++ program may replace this function.

    void* operator new[](std::size_t, const std::nothrow_t&) throw();

Allocation function similar to the one above, except that it indicates failure by returning a null pointer. A C++ program may replace this function.

    void* operator new(std::size_t, void *p) throw();

Allocation function implicitly called by the placement form of a `new` expression. Returns `p`. A C++ program may not replace this function.

    void* operator new[](std::size_t, void *p) throw();

Allocation function implicitly called by the placement array form of a `new` expression. Returns `p`. A C++ program may not replace this function.

### See Also

<new>, bad_alloc, operator delete

### Standards Conformance

ISO/IEC 14882:1998 -- International Standard for Information Systems -- Programming Language C++, Section 18.4