### C++ new Operator

The new operator provides dynamic storage allocation. The syntax for an allocation expression containing the new operator is:

    >>----------new-------------------------(type)------>
        \-::-/       \-(argument_list)-/  \-new_type-/
    >-------------------------------><
       \-(---------------------)-/
            \-initial_value-/

If you prefix new with the scope resolution operator (::), the global operator new() is used. If you specify an argument_list, the overloaded new operator that corresponds to that argument_list is used. The type is an existing built-in or user-defined type. A new_type is a type that has not already been defined and can include type specifiers and declarators.

An allocation expression containing the new operator is used to find storage in free store for the object being created. The new expression returns a pointer to the object created and can be used to initialize the object. If the object is an array, a pointer to the initial element is returned.

You can use the routine set_new_handler() to change the default behavior of new. You can also use the /Tm option to enable a debug version of new.

You cannot use the new operator to allocate function types , void, or incomplete class types because these are not object types. However, you can allocate pointers to functions with the new operator. You cannot create a reference with the new operator.

When the object being created is an array, only the first dimension can be a general expression. All subsequent dimensions must be constant integral expressions. The first dimension can be a general expression even when an existing type is used. You can create an array with zero bounds with the new operator. For example:

    char * c = new char[0];

In this case, a pointer to a unique object is returned.

An object created with operator new() exists until the operator delete() is called to deallocate the object's memory, or until program ends.

If parentheses are used within a new_type, parentheses should also surround the new_type to prevent syntax errors.

The type of the object being created cannot contain class declarations, enumeration declarations, or const or volatile types. It can contain pointers to const or volatile objects.

For example, const char* is allowed, but char* const is not.

Additional arguments can be supplied to new by using the argument_list, also called the placement syntax. If placement arguments are used, a declaration of operator new() with these arguments must exist. For example:

    #include <stddef.h>
    class X
    {
    public:
          void* operator new(size_t,int, int){ /* ... */ }
    };
    void main ()
    {
          X* ptr = new(1,2) X;
    }

Constructors and Destructors Overview

set_new_handler() - Set Behavior for new Failure
Overloaded new and delete
Free Store
delete Operator
Member Functions and the Global operator new()
Initializing Objects Created with the new Operator
Example of Allocating Storage with new()