Operator new (C++)

From RAD Studio
Jump to: navigation, search

Go Up to The new And delete Operators Index


By default, if there is no overloaded version of new, a request for dynamic memory allocation always uses the global version of new, ::operator new(). A request for array allocation calls ::operator new[](). With class objects of type name, a specific operator called name::operator new() or name::operator new[]() can be defined. When new is applied to class name objects it invokes the appropriate name::operator new if it is present; otherwise, the global ::operator new is used.

Only the operator new() function will accept an optional initializer. The array allocator version, operator new[](), will not accept initializers. In the absence of explicit initializers, the object created by new contains unpredictable data (garbage). The objects allocated by new, other than arrays, can be initialized with a suitable expression between parentheses:

   int_ptr = new int(3);

Arrays of classes with constructors are initialized with the default constructor. The user-defined new operator with customized initialization plays a key role in C++ constructors for class-type objects.

See Also