TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
new[] operator is used in a built-in C++ expression to dynamically allocate contiguous memory for an array of objects on the free store (heap). It calculates the required memory footprint, invokes the appropriate allocation function (operator new[]), sequentially initializes each element in the array, and returns a pointer to the first element.
Syntax and Initialization
Anew[] expression supports several initialization strategies depending on the syntax used. The distinction between default initialization and value initialization is particularly critical for types lacking a user-provided default constructor.
Execution Mechanics
When anew[] expression is evaluated, the C++ runtime performs the following sequence of operations:
- Size Calculation: The compiler calculates the total bytes required by multiplying the array size by
sizeof(Type). For types with non-trivial destructors, the compiler typically allocates additional hidden memory (an “array cookie”) immediately preceding the returned pointer. This cookie stores the number of elements so the runtime knows exactly how many destructors to invoke during deallocation. - Memory Allocation: The expression calls the global or class-specific allocation function,
void* operator new[](std::size_t). This function is strictly responsible for acquiring raw, uninitialized memory from the operating system. - Object Initialization: The runtime iterates through the allocated memory block and constructs each object based on the syntax provided:
- Default initialization (
new Type[size]): The default constructor is invoked. If the class lacks a user-provided default constructor, its members are left uninitialized. - Value initialization (
new Type[size]()): For classes without a user-provided default constructor, the runtime performs zero-initialization on the object before calling the implicitly-defined default constructor. For classes with a user-provided default constructor, that constructor is simply invoked. Fundamental types are zero-initialized. - List-initialization (
new Type[size]{...}): Elements are initialized in order. For aggregate types, this performs aggregate initialization. For non-aggregate classes, it invokes the appropriate constructor. Any remaining elements not covered by the initializer list are value-initialized.
- Default initialization (
Multi-dimensional and Zero-Sized Arrays
Thenew[] operator handles multi-dimensional arrays and zero-sized allocations under strict language rules:
- Multi-dimensional Arrays: When dynamically allocating multi-dimensional arrays, only the first dimension (the outermost array) can be a runtime variable. All subsequent dimensions must be constant expressions.
- Zero-Sized Arrays: Allocating an array of size zero is perfectly valid in C++. The
new[]expression returns a distinct, non-null pointer. While this pointer cannot be safely dereferenced, it represents a valid memory allocation that must be cleaned up.
Deallocation Requirement
Memory allocated via anew[] expression must strictly be deallocated using the delete[] operator.
Using the scalar delete operator on an array pointer results in undefined behavior (UB). The C++ standard provides no guarantees about program execution in this scenario; the program may crash, corrupt the heap, or terminate before any destructors are invoked.
Exception Handling
If the allocation function fails to acquire the requested memory, thenew[] expression throws a std::bad_alloc exception. If the (std::nothrow) tag is utilized, the expression suppresses the exception and instead returns a nullptr.
If an exception is thrown during the initialization phase (e.g., the constructor of the 4th element throws), the C++ runtime guarantees exception safety by automatically invoking the destructors for all fully constructed elements (elements 1 through 3) in reverse order, and then deallocating the raw memory block before propagating the exception up the call stack.
Master C++ with Deep Grasping Methodology!Learn More





