Files
2025-10-25 03:02:53 +03:00

9.9 KiB
Raw Permalink Blame History

[new.delete.array]

17 Language support library [support]

17.6 Dynamic memory management [support.dynamic]

17.6.3 Storage allocation and deallocation [new.delete]

17.6.3.3 Array forms [new.delete.array]

🔗

void* operator new[](std::size_t size); void* operator new[](std::size_t size, std::align_val_t alignment);

1

#

Effects: Theallocation functions called by the array form of anew-expression ([expr.new]) to allocatesize bytes of storage.

The second form is called for a type with new-extended alignment, and the first form is called otherwise.189

2

#

Required behavior: Same as for the corresponding single-object forms.

This requirement is binding on any replacement versions of these functions.

3

#

Default behavior: Returnsoperator new(size), oroperator new(size, alignment), respectively.

4

#

Remarks: This function is replaceable ([dcl.fct.def.replace]).

🔗

void* operator new[](std::size_t size, const std::nothrow_t&) noexcept; void* operator new[](std::size_t size, std::align_val_t alignment, const std::nothrow_t&) noexcept;

5

#

Effects: Same as above, except that these are called by a placement version of anew-expression when a C++ program prefers a null pointer result as an error indication, instead of abad_alloc exception.

6

#

Required behavior: Return a non-null pointer to suitably aligned storage ([basic.stc.dynamic]), or else return a null pointer.

Each of these nothrow versions ofoperator new[] returns a pointer obtained as if acquired from the (possibly replaced) corresponding non-placement function.

This requirement is binding on any replacement versions of these functions.

7

#

Default behavior: Calls operator new, or operator new[](size, alignment), respectively.

If the call returns normally, returns the result of that call.

Otherwise, returns a null pointer.

8

#

Remarks: This function is replaceable ([dcl.fct.def.replace]).

🔗

void operator delete[](void* ptr) noexcept; void operator delete[](void* ptr, std::size_t size) noexcept; void operator delete[](void* ptr, std::align_val_t alignment) noexcept; void operator delete[](void* ptr, std::size_t size, std::align_val_t alignment) noexcept;

9

#

Preconditions: ptr is a null pointer or its value represents the address of a block of memory allocated by an earlier call to a (possibly replaced)operator new oroperator new[](std::size_t, std::align_val_t) which has not been invalidated by an intervening call tooperator delete[].

10

#

If the alignment parameter is not present,ptr was returned by an allocation function without an alignment parameter.

If present, the alignment argument is equal to the alignment argument passed to the allocation function that returned ptr.

If present, the size argument is equal to the size argument passed to the allocation function that returned ptr.

11

#

Effects: The deallocation functions ([basic.stc.dynamic.deallocation]) called by the array form of adelete-expression to render the value of ptr invalid.

12

#

Required behavior: A call to an operator delete[] with a size parameter may be changed to a call to the corresponding operator delete[] without a size parameter, without affecting memory allocation.

[Note 1:

A conforming implementation is foroperator delete[](void* ptr, std::size_t size) to simply calloperator delete.

— end note]

13

#

Default behavior: The functions that have a size parameter forward their other parameters to the corresponding function without a size parameter.

The functions that do not have a size parameter forward their parameters to the corresponding operator delete (single-object) function.

14

#

Remarks: This function is replaceable ([dcl.fct.def.replace]).

If a replacement function without a size parameter is defined by the program, the program should also define the corresponding function with a size parameter.

If a replacement function with a size parameter is defined by the program, the program shall also define the corresponding version without the size parameter.

[Note 2:

The default behavior above might change in the future, which will require replacing both deallocation functions when replacing the allocation function.

— end note]

🔗

void operator delete[](void* ptr, const std::nothrow_t&) noexcept; void operator delete[](void* ptr, std::align_val_t alignment, const std::nothrow_t&) noexcept;

15

#

Preconditions: ptr is a null pointer or its value represents the address of a block of memory allocated by an earlier call to a (possibly replaced)operator new oroperator new[](std::size_t, std::align_val_t) which has not been invalidated by an intervening call tooperator delete[].

16

#

If the alignment parameter is not present,ptr was returned by an allocation function without an alignment parameter.

If present, the alignment argument is equal to the alignment argument passed to the allocation function that returned ptr.

17

#

Effects: The deallocation functions ([basic.stc.dynamic.deallocation]) called by the implementation to render the value of ptr invalid when the constructor invoked from a nothrow placement version of the array new-expression throws an exception.

18

#

Default behavior: Calls operator delete, or operator delete[](ptr, alignment), respectively.

19

#

Remarks: This function is replaceable ([dcl.fct.def.replace]).

189)189)

It is not the direct responsibility ofoperator new[] oroperator delete[] to note the repetition count or element size of the array.

Those operations are performed elsewhere in the arraynew anddelete expressions.

The arraynew expression, can, however, increase the size argument tooperator new[] to obtain space to store supplemental information.