1. Motivation
Throughout this document "malloc" refers to the implementation of:: operator  new Everyone’s favorite dynamic data structure, 
void vector :: reserve ( size_t new_cap ) { if ( capacity_ >= new_cap ) return ; const size_t bytes = new_cap ; void * newp = :: operator new ( new_cap ); memcpy ( newp , ptr_ , capacity_ ); ptr_ = newp ; capacity_ = bytes ; } 
Consider the sequence of calls:
std :: vector < char > v ; v . reserve ( 37 ); // ... v . reserve ( 38 ); 
All reasonable implementations of malloc round sizes, both for alignment requirements and improved performance. It is extremely unlikely that malloc provided us exactly 37 bytes. We do not need to invoke the allocator here...except that we don’t know that for sure, and to use the 38th byte would be undefined behavior. We would like that 38th byte to be usable without a roundtrip through the allocator.
This paper proposes an API making it safe to use that byte, and explores many of the design choices (not all of which are obvious without implementation experience.)
1.1. nallocx: not as awesome as it looks
The simplest way to help here is to provide an informative API answering the
question "If I ask for N bytes, how many do I actually get?" [jemalloc] calls
this 
void vector :: reserve ( size_t new_cap ) { if ( capacity_ >= new_cap ) return ; const size_t bytes = nallocx ( new_cap , 0 ); void * newp = :: operator new ( bytes ); memcpy ( newp , ptr_ , capacity_ ); ptr_ = newp ; capacity_ = bytes ; } 
This is a good start, and does in fact work to allow vector and friends to use the true extent of returned objects. But there are three significant problems with this approach.
1.1.1. nallocx must give a conservative answer
While many allocators have a deterministic map from requested size to allocated
size, it is by no means guaranteed that all do.  Presumably they can make a
reasonably good guess, but if two calls to 
1.1.2. nallocx duplicates work
Allocation is often a crucial limit on performance.  Most allocators compute
the returned size of an object as part of fulfilling that allocation...but if
we make a second call to 
1.1.3. nallocx hides information from malloc
The biggest problem (for the authors) is that 
Google’s malloc implementation (TCMalloc) rounds requests to one of a small (<100) number of sizeclasses: we maintain local caches of appropriately sized objects, and cannot do this for every possible size of object. Originally, these sizeclasses were just reasonably evenly spaced among the range they cover. Since then, we have used extensive telemetry on allocator use in the wild to tune these choices. In particular, as we know (approximately) how many objects of any given size are requested, we can solve a fairly simple optimization problem to minimize the total internal fragmentation for any choice of N sizeclasses.
Widespread use of 
Note that we can’t take the same telemetry from 
Optimization guided by malloc telemetry has been one of our most effective
tools in improving allocator performance.  It is important that we fix this
issue without losing the ground truth of what a caller of 
These three issues explain why we don’t believe 
1.2. after allocation is too late
Another obvious suggestion is to add a way to inspect the size of an object
returned by 
void vector :: reserve ( size_t new_cap ) { if ( capacity_ >= new_cap ) return ; void * newp = :: operator new ( new_cap ); const size_t bytes = sallocx ( newp ); memcpy ( newp , ptr_ , capacity_ ); ptr_ = newp ; capacity_ = bytes ; } 
This is worse than nallocx. It fixes the non-constant size problem, and avoids
a feedback loop, but the performance issue is worse (this is the major issue fixed by [SizedDelete]!), and what’s worse, the above code invokes UB as
soon as we touch byte 
1.3. realloc’s day has passed
We should also quickly examine why the classic C API 
void vector :: reserve ( size_t new_cap ) { if ( capacity_ >= new_cap ) return ; ptr_ = realloc ( ptr_ , new_cap ); capacity_ = new_cap ; } 
In principle a realloc from 37 to 38 bytes wouldn’t carry the full cost of allocation. But it’s dramatically more expensive than making no call at all. What’s more, there are a number of more complicated dynamic data structures that store variable-sized chunks of data but are never actually resized. These data structures still deserve the right to use all the memory they’re paying for.
Furthermore, 
2. Proposal
We propose adding new overloads of 
We propose wording, relative to [N4791]:
- 
     Amend [basic.stc.dynamic.allocation] (6.6.5.4.1) paragraph 1: 
An allocation function shall be a class member function or a global function; a program is ill-formed if an allocation function is declared in a namespace scope other than global scope or declared static in global scope.An allocation function is a size-returning allocation function if it has a second parameter of type
The return type shall be, or it has a second parameter of typestd :: return_size_t and a third parameter of typestd :: align_val_t .std :: return_size_t if the allocation function is a size-returning allocation function andstd :: sized_ptr_t otherwise . The first parameter shall have typevoid * ([support.types]). The first parameter shall not have an associated default argument ([dcl.fct.default]). The value of the first parameter is interpreted as the requested size of the allocation. [...]std :: size_t 
- 
     Amend [expr.new] (7.6.2.4) paragraph 5: 
Objects created by a new-expression have dynamic storage duration ([basic.stc.dynamic]). [ Note: The lifetime of such an object is not necessarily restricted to the scope in which it is created. — end note ]When the allocated object is not an array, the result of the new-expression is
- if the new-expression is a size-returning placement new expression (see below), an object of type
([new.syn]) whosestd :: return_size_t member points to the object created and whosep member is then member of the value returned by the size-returning allocation function;n - otherwise, a pointer to the object created.
- 
     Amend [expr.new] (7.6.2.4) paragraph 6: 
When the allocated object is an array (that is, the noptr-new-declarator syntax is used or the new-type-id or type-id denotes an array type), the new-expression yields
- if the new-expression is a size-returning placement new expression (see below), an object of type
([new.syn]) whosestd :: return_size_t member points to the object created and whosep member is then member of the value returned by the size-returning allocation function less any array allocation overhead;n - otherwise, a pointer to the initial element (if any) of the array.
[ Note: Both
andnew int have typenew int [ 10 ] and the type ofint * isnew int [ i ][ 10 ] — end note ] The attribute-specifier-seq in a noptr-new-declarator appertains to the associated array type.int ( * )[ 10 ] 
- 
     Amend [expr.new] (7.6.2.4) paragraph 15: 
Overload resolution is performed on a function call created by assembling an argument list. The first argument is the amount of space requested, and has type. If the type of the allocated object has new-extended alignment, the next argument is the type’s alignment, and has typestd :: size_t . If the new-placement syntax is used, the initializer-clauses in its expression-list are the succeeding arguments. If no matching function is found and the allocated object type has new-extended alignment, the alignment argument is removed from the argument list, and overload resolution is performed again. A size-returning placement new expression is one whose selected allocation function is a size-returning allocation function.std :: align_val_t 
- 
     Amend [expr.new] (7.6.2.4) paragraph 25: 
If a new-expression calls a deallocation function, it passes
- the
member of the object returned by the size-returning allocation function;p - otherwise, the value returned from the allocation function call
as the first argument of type
. If a placement deallocation function is called, it is passed the same additional arguments as were passed to the placement allocation function, that is, the same arguments as those specified with the new-placement syntax. If no matching function is found and one of the arguments to the new-placement syntax has typevoid * , that argument is removed from the argument list, and overload resolution is performed again. If the implementation is allowed to introduce a temporary object or make a copy of any argument as part of the call to the allocation function, it is unspecified whether the same object is used in the call to both the allocation and deallocation functions.std :: return_size_t 
- 
     Amend [replacement.functions] (15.5.4.6) paragraph 2: 
operator new ( std :: size_t ) operator new ( std :: size_t , std :: align_val_t ) operator new ( std :: size_t , const std :: nothrow_t & ) operator new ( std :: size_t , std :: align_val_t , const std :: nothrow_t & ) operator new ( size_t size , std :: return_size_t ) operator new ( size_t size , std :: align_val_t al , std :: return_size_t ) operator new ( size_t size , std :: return_size_t , std :: nothrow_t ) operator new ( size_t size , std :: align_val_t al , std :: return_size_t , std :: nothrow_t ) [...] operator new []( std :: size_t ) operator new []( std :: size_t , std :: align_val_t ) operator new []( std :: size_t , const std :: nothrow_t & ) operator new []( std :: size_t , std :: align_val_t , const std :: nothrow_t & ) operator new []( size_t size , std :: return_size_t ) operator new []( size_t size , std :: align_val_t al , std :: return_size_t ) operator new []( size_t size , std :: return_size_t , std :: nothrow_t ) operator new []( size_t size , std :: align_val_t al , std :: return_size_t , std :: nothrow_t ) [...] 
- 
     Amend header < new > 
namespace std { class bad_alloc ; class bad_array_new_length ; struct destroying_delete_t { explicit destroying_delete_t () = default ; }; inline constexpr destroying_delete_t destroying_delete {}; struct return_size_t { explicit return_size_t () = default ; }; inline constexpr return_size_t return_size {}; template < typename T = void > struct std :: sized_ptr_t { T * p ; size_t n ; }; enum class align_val_t : size_t {}; [...] } [[ nodiscard ]] void * operator new ( std :: size_t size ); [[ nodiscard ]] void * operator new ( std :: size_t size , std :: align_val_t alignment ); [[ nodiscard ]] void * operator new ( std :: size_t size , const std :: nothrow_t & ) noexcept ; [[ nodiscard ]] void * operator new ( std :: size_t size , std :: align_val_t alignment , const std :: nothrow_t & ) noexcept ; [[ nodiscard ]] std :: sized_ptr_t :: operator new ( size_t size , std :: return_size_t ); [[ nodiscard ]] std :: sized_ptr_t :: operator new ( size_t size , std :: align_val_t alignment , std :: return_size_t ); [[ nodiscard ]] std :: sized_ptr_t :: operator new ( size_t size , std :: return_size_t , std :: nothrow_t ) noexcept ; [[ nodiscard ]] std :: sized_ptr_t :: operator new ( size_t size , std :: align_val_t alignment , std :: return_size_t , std :: nothrow_t ) noexcept ; [...] [[ nodiscard ]] void * operator new []( std :: size_t size ); [[ nodiscard ]] void * operator new []( std :: size_t size , std :: align_val_t alignment ); [[ nodiscard ]] void * operator new []( std :: size_t size , const std :: nothrow_t & ) noexcept ; [[ nodiscard ]] void * operator new []( std :: size_t size , std :: align_val_t alignment , const std :: nothrow_t & ) noexcept ; [[ nodiscard ]] std :: sized_ptr_t :: operator new []( size_t size , std :: return_size_t ); [[ nodiscard ]] std :: sized_ptr_t :: operator new []( size_t size , std :: align_val_t alignment , std :: return_size_t ); [[ nodiscard ]] std :: sized_ptr_t :: operator new []( size_t size , std :: return_size_t , std :: nothrow_t ) noexcept ; [[ nodiscard ]] std :: sized_ptr_t :: operator new []( size_t size , std :: align_val_t alignment , std :: return_size_t , std :: nothrow_t ) noexcept ; [...] [[ nodiscard ]] void * operator new ( std :: size_t size , void * ptr ) noexcept ; [[ nodiscard ]] void * operator new []( std :: size_t size , void * ptr ) noexcept ; void operator delete ( void * ptr , void * ) noexcept ; void operator delete []( void * ptr , void * ) noexcept ; 
- 
     Amend [new.delete.single] (16.6.2.1) 
[[ nodiscard ]] std :: sized_ptr_t :: operator new ( size_t size , std :: return_size_t ); [[ nodiscard ]] std :: sized_ptr_t :: operator new ( size_t size , std :: align_val_t alignment , std :: return_size_t ); 
Effects: Same as above, except these are called by a placement version of a new-expression when a C++ program prefers the size-returning allocation function.
Replaceable: A C++ program may define functions with either of these function signatures, and thereby displace the default versions defined by the C++ standard library.
Required behavior: Return a sized_ptr_t whose
member represents the address of a region of N bytes of suitably aligned storage ([basic.stc.dynamic]) for some N >=p , and whosesize member is N, or else throw an exception. This requirement is binding on any replacement versions of these functions.bad_alloc 
Default behavior: Returns
andstd :: sized_ptr_t { operator new ( size ), N } respectively. If a user-provided operator new is invoked directly or indirectly, N isstd :: sized_ptr_t { operator new ( size , alignment ), N } .size [[ nodiscard ]] std :: sized_ptr_t :: operator new ( size_t size , std :: return_size_t , std :: nothrow_t ) noexcept ; [[ nodiscard ]] std :: sized_ptr_t :: operator new ( size_t size , std :: align_val_t alignment , std :: return_size_t , std :: nothrow_t ) noexcept ; 
Effects: Same as above, except these are called by a placement version of a new-expression when a C++ program prefers the size-returning allocation function and a null pointer result as an error indication.
Replaceable: A C++ program may define functions with either of these function signatures, and thereby displace the default versions defined by the C++ standard library.
Required behavior: Return a sized_ptr_t whose
member represents the address of a region of N bytes of suitably aligned storage ([basic.stc.dynamic]) for some N >=p , and whosesize member is N, or else returnn . Each of these nothrow versions ofstd :: sized_ptr_t { nullptr , 0 } 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.operator new 
Default behavior: Returns
andstd :: sized_ptr_t { operator new ( size ), N } respectively. If a user-provided operator new is invoked directly or indirectly, N isstd :: sized_ptr_t { operator new ( size , alignment ), N } . If the call tosize throws, returnsoperator new .std :: sized_ptr_t { nullptr , 0 } 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 ; [...]
Requires: If the
parameter is not present,alignment shall have been returned by an allocation function without an alignment parameter. If present, theptr argument shall equal the alignment argument passed to the allocation function that returnedalignment . If present, theptr argument shall equal thesize argument passed to the allocation function that returnedsize , ifptr was not allocated by a size-returning allocation function. If present, theptr argument shall satisifysize ifn >= size >= requested was allocated by a size-returning allocation function, whereptr is the size returned inn andstd :: sized_ptr_t is the size argument passed to the allocation function .requested [...]
- 
     Amend [new.delete.array] (16.6.2.2) 
[[ nodiscard ]] std :: sized_ptr_t :: operator new []( size_t size , std :: return_size_t ); [[ nodiscard ]] std :: sized_ptr_t :: operator new []( size_t size , std :: align_val_t alignment , std :: return_size_t ); 
Effects: Same as above, except these are called by a placement version of a new-expression when a C++ program prefers the size-returning allocation function.
Replaceable: A C++ program may define functions with either of these function signatures, and thereby displace the default versions defined by the C++ standard library.
Required behavior: Return a sized_ptr_t whose
member represents the address of a region of N bytes of suitably aligned storage ([basic.stc.dynamic]) for some N >=p , and whosesize member is N, or else throw an exception. This requirement is binding on any replacement versions of these functions.bad_alloc 
Default behavior: Returns
andstd :: sized_ptr_t { operator new []( size ), N } respectively. If a user-provided operator new is invoked directly or indirectly, N isstd :: sized_ptr_t { operator new []( size , alignment ), N } .size [[ nodiscard ]] std :: sized_ptr_t :: operator new []( size_t size , std :: return_size_t , std :: nothrow_t ) noexcept ; [[ nodiscard ]] std :: sized_ptr_t :: operator new []( size_t size , std :: align_val_t alignment , std :: return_size_t , std :: nothrow_t ) noexcept ; 
Effects: Same as above, except these are called by a placement version of a new-expression when a C++ program prefers the size-returning allocation function and a null pointer result as an error indication.
Replaceable: A C++ program may define functions with either of these function signatures, and thereby displace the default versions defined by the C++ standard library.
Required behavior: Return a sized_ptr_t whose
member represents the address of a region of N bytes of suitably aligned storage ([basic.stc.dynamic]) for some N >=p , and whosesize member is N, or else returnn . This requirement is binding on any replacement versions of these functions.std :: sized_ptr_t { nullptr , 0 } 
Default behavior: Returns
andstd :: sized_ptr_t { operator new []( size ), N } respectively. If a user-provided operator new is invoked directly or indirectly, N isstd :: sized_ptr_t { operator new []( size , alignment ), N } . If the call tosize throws, returnsoperator new [] .std :: sized_ptr_t { nullptr , 0 } 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 ; [...]
Requires: If the
parameter is not present,alignment shall have been returned by an allocation function without anptr parameter. If present, thealignment argument shall equal thealignment argument passed to the allocation function that returnedalignment . If present, theptr argument shall equal thesize argument passed to the allocation function that returnedsize , ifptr was not allocated by a size-returning allocation function. If present, theptr argument shall satisifysize ifn >= size >= requested was allocated by a size-returning allocation function, whereptr is the size returned inn andstd :: sized_ptr_t is the size argument passed to the allocation function .requested [...]
- 
     Amend Table 17 ("Feature-test macros") [cpp.predefined] (15.10) 
Name Value __cpp_size_returning_new PLACEHOLDER DATE 
3. Alternative Designs Considered
Another signature we could use would be:
enum class return_size_t : std :: size_t {}; void * :: operator new ( size_t size , std :: return_size_t & ); 
(and so on.) This is slightly simpler to read as a signature, but arguably worse in usage:
std :: tie ( obj . ptr , obj . size ) = :: operator new ( 37 , std :: return_size_t {}); // ...vs... // Presumably the object implementation wants to contain a size_t, // not a return_size_t. std :: return_size_t rs ; obj . ptr = :: operator new ( 37 , rs ); obj . size = rs ; 
More importantly, this form is less efficient. In practice, underlying malloc
implementations provide actual definitions of 
- 
     Linux ABIs support returning at least two scalar values in registers (even if they’re members of a trivially copyable struct) which can be dramatically more efficient. 
- 
     The [MicrosoftABI] returns large types by pointer, but this is no worse than making the reference parameter an inherent part of the API. 
Whether we use a reference parameter or a second returned value, the interpretation is the same.
3.1. How many :: operator  new 
   It is unfortunate that we have so many permutations of 
The authors have considered other alternatives to the additional overloads. At the Jacksonville meeting, EWG suggested looking at parameter packs.
- 
     Parameter packs do not reduce the number of symbols introduced. Implementers still need to provide implementations each of the n overloads. 
- 
     Retrofitting parameter packs leaves us with more mangled variants. Implementers need to provide both the legacy symbols as well as the parameter pack-mangled symbols. 
The authors have also considered APIs where all parameters are passed, thereby
requiring a single new overload.  This adds further overhead for
implementations, as it moves compile-time decisions (is the alignment at or
below the minimum guaranteed by 
The alternative to modifying the handling of new-expressions invoking
deallocation functions (when an exception is thrown) would require additional
overloads for 
3.2. Implementation difficulty
It’s worth reiterating that there’s a perfectly good trivial implementation of these functions:
std :: sized_ptr_t :: operator new ( size_t n , std :: return_size_t ) { return { :: operator new ( n ), n }; } 
Malloc implementations are free to properly override this with a more impactful definition, but this paper poses no significant difficulty for toolchain implementers.
Implementation Experience:
- 
     TCMalloc has developed a (currently internal) implementation. While this requires mapping from an integer size class to the true number of bytes, combining this lookup with the allocation is more efficient as we avoid recomputing the sizeclass itself (given a request) or deriving it from the object’s address. 
- 
     jemalloc is prototyping a smallocx 
3.3. Interaction with Sized Delete
For allocations made with 
Consider the memory allocated by:
using T = std :: aligned_storage < 16 , 8 >:: type ; std :: vector < T > v ( 4 ); 
The underlying heap allocation is made with 
- 
     The memory allocator may return a 72 byte object: Since there is no k sizeof ( T ) * k = 72 :: operator delete ( void * , size_t ) 
- 
     The memory allocator may instead return an 80 byte object (5 T 
For allocations made with
std :: tie ( p , m ) = :: operator new ( n , std :: return_size_t {}); 
we permit 
This behavior is consistent with [jemalloc]'s 
3.4. Advantages
It’s easy to see that this approach nicely solves the problems posed by other methods:
- 
     We pay almost nothing in speed to return an actual-size parameter. For TCMalloc and jemalloc, this is typically a load from to map from sizeclass to size. This cost is strictly smaller than with nallocx 
- 
     We are told exactly the size we have, without risk of UB. We can avoid subsequent reallocations when growing to a buffer to an already-allocated size. 
- 
     Allocator telemetry knows actual request sizes exactly. 
4. New Expressions
Additionally, we propose expanding this functionality to 
- 
     For new 
- 
     For new [] auto [ p , sz ] = new ( std :: return_size ) T [ 5 ]; for ( int i = 5 ; i < sz / sizeof ( T ); i ++ ) { new ( p [ i ]) T ; } for ( int i = 0 ; i < sz / sizeof ( T ); i ++ ) { p [ i ]. DoStuff (); } for ( int i = 5 ; i < sz / sizeof ( T ); i ++ ) { p [ i ]. ~ T (); } delete [] p ; 
We considered alternatives for returning the size.
- 
     We could return two pointers, the initial object and one past the end of the array (minus the array allocation overhead). auto [ start , end ] = new ( std :: return_size ) T [ 5 ]; for ( T * p = start + 5 ; p != end ; p ++ ) { new ( p ) T ; } for ( T * p = start ; p != end ; p ++ ) { p -> DoStuff (); } for ( T * p = start + 5 ; p != end ; p ++ ) { p ->~ T (); } delete [] start ; The pair of pointers provides convience for use with iterator-oriented algorithms. The problem we foresee is that a size-returning allocation function may not provide a size that is an appropriate multiple of sizeof ( T ) alignof ( T ) 
- 
     We could return the size in units of T new new [] - 
       For new T T :: operator new 
- 
       For new [] T 
 
- 
       
- 
     We could pass the size in units of T T - 
       For new T 
- 
       For new [] char int 
 
- 
       
- 
     We could return the size via a reference parameter: std :: return_end < T > end ; T * p = new ( end ) T [ 5 ]; for ( T * p = start + 5 ; p != end ; p ++ ) { new ( p ) T ; } for ( T * p = start ; p != end ; p ++ ) { p -> DoStuff (); } for ( T * p = start + 5 ; p != end ; p ++ ) { p ->~ T (); } or, demonstrated with bytes: std :: return_size_t size ; T * p = new ( size ) T [ 5 ]; for ( int i = 5 ; i < size / sizeof ( T ); i ++ ) { new ( p [ i ]) T ; } for ( int i = 0 ; i < size / sizeof ( T ); i ++ ) { p [ i ]. DoStuff (); } for ( int i = 5 ; i < size / sizeof ( T ); i ++ ) { p [ i ]. ~ T (); } delete [] p ; (Casts omitted for clarity.) As discussed for :: operator new 
For 
- 
     This would avoid the need to explicitly construct / destruct the elements with the additional returned space (if any). The new-initializer is invoked for the returned number of elements, rather than the requested number of elements. This allows delete [] sz / sizeof ( T ) 
- 
     The presented proposal (leaving this space uninitialized) was chosen for consistency with new 
5. Related work
[P0401R1] considers this problem at the level of the 
6. History
6.1. R4 → R5
EWG reviewed P0901R4 at [Cologne].
Poll: P0901R4 as presented, forward to LEWG for C++23, not C++20.
SF F N A SA 2 11 14 2 0 
- 
     Fixed typos in examples. 
- 
     Added proposed feature test macro. 
- 
     Added LEWG audience for library support type names. 
6.2. R3 → R4
- 
     Update reference to revised [P0401R1]. 
6.3. R2 → R3
- 
     Added proposed wording. 
- 
     For newly added allocation functions, std :: nothrow_t 
6.4. R1 → R2
Applied feedback from San Diego Mailing
- 
     Moved from passing std :: return_size_t 
- 
     Added rationale for not using parameter packs for this functionality. 
6.5. R0 → R1
Applied feedback from [JacksonvilleMinutes].
- 
     Clarified in §2 Proposal the desire to leverage the existing "replacement functions" wording of the IS, particularly given the close interoperation with the existing :: operator new :: operator delete 
- 
     Added a discussion of the Microsoft ABI in §2 Proposal. 
- 
     Noted in §3.1 How many ::operator new's? the possibility of using a parameter pack. 
- 
     Added a proposal for §4 New Expressions, as requested by EWG. 
Additionally, a discussion of §3.3 Interaction with Sized Delete has been added.