Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      operator new, operator new[]

      From cppreference.com
      <cpp‎ |memory‎ |new
       
       
      Utilities library
       
      Memory management library
      (exposition only*)
      Allocators
      Uninitialized memory algorithms
      Constrained uninitialized memory algorithms
      Memory resources
      Uninitialized storage(until C++20)
      (until C++20*)
      (until C++20*)
      Garbage collector support(until C++23)
      (C++11)(until C++23)
      (C++11)(until C++23)
      (C++11)(until C++23)
      (C++11)(until C++23)
      (C++11)(until C++23)
      (C++11)(until C++23)
       
       
      Defined in header<new>
      Replaceable allocation functions
      void* operator new  (std::size_t count);
      (1)
      void* operator new[](std::size_t count);
      (2)
      void* operator new  (std::size_t count,std::align_val_t al);
      (3)(since C++17)
      void* operator new[](std::size_t count,std::align_val_t al);
      (4)(since C++17)
      Replaceable non-throwing allocation functions
      void* operator new  (std::size_t count,conststd::nothrow_t& tag);
      (5)(noexcept since C++11)
      void* operator new[](std::size_t count,conststd::nothrow_t& tag);
      (6)(noexcept since C++11)
      void* operator new  (std::size_t count,std::align_val_t al,
                           conststd::nothrow_t& tag)noexcept;
      (7)(since C++17)
      void* operator new[](std::size_t count,std::align_val_t al,
                           conststd::nothrow_t& tag)noexcept;
      (8)(since C++17)
      Non-allocating placement allocation functions
      void* operator new  (std::size_t count,void* ptr);
      (9)(noexcept since C++11)
      (constexpr since C++26)
      void* operator new[](std::size_t count,void* ptr);
      (10)(noexcept since C++11)
      (constexpr since C++26)
      User-defined placement allocation functions
      void* operator new  (std::size_t count,/* args... */);
      (11)
      void* operator new[](std::size_t count,/* args... */);
      (12)
      void* operator new  (std::size_t count,
                           std::align_val_t al,/* args... */);
      (13)(since C++17)
      void* operator new[](std::size_t count,
                           std::align_val_t al,/* args... */);
      (14)(since C++17)
      Class-specific allocation functions
      void* T::operator new  (std::size_t count);
      (15)
      void* T::operator new[](std::size_t count);
      (16)
      void* T::operator new  (std::size_t count,std::align_val_t al);
      (17)(since C++17)
      void* T::operator new[](std::size_t count,std::align_val_t al);
      (18)(since C++17)
      Class-specific placement allocation functions
      void* T::operator new  (std::size_t count,/* args... */);
      (19)
      void* T::operator new[](std::size_t count,/* args... */);
      (20)
      void* T::operator new  (std::size_t count,
                               std::align_val_t al,/* args... */);
      (21)(since C++17)
      void* T::operator new[](std::size_t count,
                               std::align_val_t al,/* args... */);
      (22)(since C++17)

      Attempts to allocate requested number of bytes, and the allocation request can fail (even if the requested number of bytes is zero). These allocation functions are called bynew expressions to allocate memory in which new object would then be initialized. They may also be called using regular function call syntax.

      1-8)Replaceable allocation functions. The standard library provides default implementations for these functions, for the effects of the default implementations, seebelow.
      9,10) Called by the standardplacementnew expressions. Performs no action and returnsptr unmodified.
      If this function is called through placementnew andptr is a null pointer, the behavior is undefined.
      11-22) User-defined allocation functions called bynew expressions.

      Overloads(1-4) are implicitly declared in each translation unit even if the<new> header is not included.

      Seenew expression for the criteria of selecting overload.

      Contents

      [edit]Parameters

      count - number of bytes to allocate
      ptr - pointer to a memory area to initialize the object at
      tag - disambiguation tag used to select non-throwing overloads
      al - alignment to use, invalid value leads to undefined behavior

      [edit]Return value

      1-4) If the allocation succeeds, a non-null pointerp0 which points to suitably aligned memory of size at leastsize and is different from any previously returned valuep1, unless that valuep1 was subsequently passed to a replaceabledeallocation function; if the allocation fails, does not return (an exception is thrown, see below).
      5-8) Same as(1-4), but returns a null pointer if the allocation fails.
      9,10)ptr
      11-22) Same as(1-4) if the function does not return on allocation failure, otherwise same as(5-8).

      [edit]Exceptions

      1-4) Throws an exception of a type that would match a handler of typestd::bad_alloc on failure to allocate memory.
      11-22) Same as(1-4) if the function does not return on allocation failure, otherwise same as(5-8).

      [edit]Global replacements

      Overloads(1-8) arereplaceable. The effects of the default versions are:

      1) Attempts to allocate the requested storage. Whether the attempt involves a call tostd::malloc orstd::aligned_alloc is unspecified.
      • If the attempt is successful, returns a pointer to the allocated storage.
      • Otherwise, if currently nonew-handler is installed, throwsstd::bad_alloc.
      • Otherwise, calls the currently installed new-handler.
        • If the new-handler returns, starts another allocation attempt.
        • Otherwise, exits the current invocation.
      2) Returnsoperator new(count).
      3) Same as(1).
      4) Returnsoperator new(count, al).
      5-8) Calls(1-4) respectively with the same arguments except fortag.
      • If the call returns normally, returns the result of that call.
      • Otherwise, returns a null pointer.

      Onfreestanding implementations, it is implementation-defined whether the default versions of(1-8) satisfy the behaviors required above. Freestanding implementations are recommended that if any of these default versions meet the requirements of a hosted implementation, they all should.

      (since C++26)

      Globaloperatorsnew/delete replacement:

      Run this code
      #include <cstdio>#include <cstdlib>#include <new> // no inline, required by [replacement.functions]/3void* operator new(std::size_t sz){std::printf("1) new(size_t), size = %zu\n", sz);if(sz==0)++sz;// avoid std::malloc(0) which may return nullptr on success if(void*ptr=std::malloc(sz))return ptr; throwstd::bad_alloc{};// required by [new.delete.single]/3} // no inline, required by [replacement.functions]/3void* operator new[](std::size_t sz){std::printf("2) new[](size_t), size = %zu\n", sz);if(sz==0)++sz;// avoid std::malloc(0) which may return nullptr on success if(void*ptr=std::malloc(sz))return ptr; throwstd::bad_alloc{};// required by [new.delete.single]/3} voidoperator delete(void* ptr)noexcept{std::puts("3) delete(void*)");std::free(ptr);} voidoperator delete(void* ptr,std::size_t size)noexcept{std::printf("4) delete(void*, size_t), size = %zu\n", size);std::free(ptr);} voidoperator delete[](void* ptr)noexcept{std::puts("5) delete[](void* ptr)");std::free(ptr);} voidoperator delete[](void* ptr,std::size_t size)noexcept{std::printf("6) delete[](void*, size_t), size = %zu\n", size);std::free(ptr);} int main(){int* p1= newint;    delete p1; int* p2= newint[10];// guaranteed to call the replacement in C++11    delete[] p2;}

      Possible output:

      // Compiled with GCC-5 in C++17 mode to obtain the following:1) op new(size_t), size = 44) op delete(void*, size_t), size = 42) op new[](size_t), size = 405) op delete[](void* ptr)

      Overloads ofoperator new andoperator new[] with additional user-defined parameters ("placement forms", versions(11-14)) may be declared at global scope as usual, and are called by the matchingplacement forms ofnew expressions.

      The standard library's non-allocating placement forms ofoperator new(9,10) cannot be replaced and can only be customized if the placementnew expression did not use the::new syntax, by providing a class-specific placementnew(19,20) with matching signature:void* T::operator new(std::size_t,void*) orvoid* T::operator new[](std::size_t,void*).

      The placement formvoid* operator new(std::size_t,std::size_t) is not allowed because the matching signature of the deallocation function,voidoperator delete(void*,std::size_t), is a usual (not placement) deallocation function.

      (since C++14)

      [edit]Class-specific overloads

      Both single-object and array allocation functions may be defined as public static member functions of a class (versions(15-18)). If defined, these allocation functions are called bynew expressions to allocate memory for single objects and arrays of this class, unless thenew expression used the form::new which bypasses class-scope lookup. The keywordstatic is optional for these functions: whether used or not, the allocation function is a static member function.

      Thenew expression looks for appropriate allocation function's name firstly in the class scope, and after that in the global scope. Note, that as pername lookup rules, any allocation functions declared in class scope hides all global allocation functions for thenew expressions that attempt to allocate objects of this class.

      When allocating objects and arrays of objects whose alignment exceeds__STDCPP_DEFAULT_NEW_ALIGNMENT__, overload resolution is performed twice: first, for alignment-aware function signatures, then for alignment-unaware function signatures. This means that if a class with extended alignment has an alignment-unaware class-specific allocation function, it is the function that will be called, not the global alignment-aware allocation function. This is intentional: the class member is expected to know best how to handle that class.

      (since C++17)

      When allocating objects and arrays of objects whose alignment does not exceed__STDCPP_DEFAULT_NEW_ALIGNMENT__, overload resolution is performed twice: first, for alignment-unaware function signatures, then for alignment-aware function signatures.

      (since C++20)
      Run this code
      #include <cstddef>#include <iostream> // class-specific allocation functionsstruct X{staticvoid* operator new(std::size_t count){std::cout<<"custom new for size "<< count<<'\n';return::operator new(count);} staticvoid* operator new[](std::size_t count){std::cout<<"custom new[] for size "<< count<<'\n';return::operator new[](count);}}; int main(){    X* p1= new X;    delete p1;    X* p2= new X[10];    delete[] p2;}

      Possible output:

      custom new for size 1custom new[] for size 10

      Overloads ofoperator new andoperator new[] with additional user-defined parameters ("placement forms"), may also be defined as class members(19-22)). When the placementnew expression with the matching signature looks for the corresponding allocation function to call, it begins at class scope before examining the global scope, and if the class-specific placementnew is provided, it is called.

      When allocating objects and arrays of objects whose alignment exceeds__STDCPP_DEFAULT_NEW_ALIGNMENT__, overload resolution for placement forms is performed twice just as for regular forms: first, for alignment-aware function signatures, then for alignment-unaware function signatures.

      (since C++17)

      When allocating objects and arrays of objects whose alignment does not exceed__STDCPP_DEFAULT_NEW_ALIGNMENT__, overload resolution for placement forms is performed twice just as for regular forms: first, for alignment-unaware function signatures, then for alignment-aware function signatures.

      (since C++20)
      Run this code
      #include <cstddef>#include <iostream>#include <stdexcept> struct X{    X(){throwstd::runtime_error("");} // custom placement newstaticvoid* operator new(std::size_t count,bool b){std::cout<<"custom placement new called, b = "<< b<<'\n';return::operator new(count);} // custom placement deletestaticvoidoperator delete(void* ptr,bool b){std::cout<<"custom placement delete called, b = "<< b<<'\n';::operator delete(ptr);}}; int main(){try{[[maybe_unused]] X* p1= new(true) X;}catch(conststd::exception&){}}

      Output:

      custom placement new called, b = 1custom placement delete called, b = 1

      If class-leveloperator new is a template function, it must have the return type ofvoid*, the first argumentstd::size_t, and it must have two or more parameters. In other words, only placement forms can be templates.

      [edit]Notes

      Even though the non-allocating placementnew(9,10) cannot be replaced, a function with the same signature may be defined at class scope as described above. In addition, global overloads that look like placementnew but take a non-void pointer type as the second argument are allowed, so the code that wants to ensure that the true placementnew is called (e.g.std::allocator::construct), must use::new and also cast the pointer tovoid*.

      If the behavior of a deallocation function does not satisfy the default constraints, the behavior is undefined.

      The following functions are required to be thread-safe:

      Calls to these functions that allocate or deallocate a particular unit of storage occur in a single total order, and each such deallocation callhappens-before the next allocation (if any) in this order.

      (since C++11)

      It is unspecified whether library versions ofoperator new make any calls tostd::malloc orstd::aligned_alloc(since C++17).

      For loading a large file, file mapping via OS-specific functions, e.g.,mmap on POSIX orCreateFileMapping(A/W) along withMapViewOfFile on Windows, is preferable to allocating a buffer for file reading.

      Feature-test macroValueStdFeature
      __cpp_lib_freestanding_operator_new202306L(C++26)freestanding support for replaceableoperator new[1]
      0(C++26)no freestanding support
      __cpp_lib_constexpr_new202406L(C++26)constexpr placementnew andnew[]
      1. Formally, this macro expands to202306L if all the default versions of the replaceable global allocation functions meet the requirements of a hosted implementation.

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      CWG 521C++98any class derived fromstd::bad_alloc could be thrown,
      even if thestd::bad_alloc base is ambiguous or inaccessible
      the exception thrown should match
      a handler of typestd::bad_alloc
      LWG 9C++98multiple calls for allocating zero
      bytes could yield the same pointer
      only allowed if all such previously
      yielded pointers have been
      passed to deallocation functions
      LWG 206C++98replacing the replaceable allocation functions did
      not affect the default behaviors of the corresponding
      replaceable non-throwing allocation functions
      the default behaviors
      change accordingly
      LWG 404C++98replacements of the replaceable allocation
      functions could be declaredinline
      prohibited, no diagnostic required

      [edit]References

      • C++23 standard (ISO/IEC 14882:2024):
      • 17.7 Dynamic memory management [support.dynamic]
      • C++20 standard (ISO/IEC 14882:2020):
      • 17.6 Dynamic memory management [support.dynamic]
      • C++17 standard (ISO/IEC 14882:2017):
      • 21.6 Dynamic memory management [support.dynamic]
      • C++14 standard (ISO/IEC 14882:2014):
      • 18.6 Dynamic memory management [support.dynamic]
      • C++11 standard (ISO/IEC 14882:2011):
      • 18.6 Dynamic memory management [support.dynamic]
      • C++03 standard (ISO/IEC 14882:2003):
      • 18.4 Dynamic memory management [lib.support.dynamic]
      • C++98 standard (ISO/IEC 14882:1998):
      • 18.4 Dynamic memory management [lib.support.dynamic]

      [edit]See also

      [static](C++23)
      allocates memory usingAllocator
      (public static member function ofstd::generator<Ref,V,Allocator>::promise_type)[edit]
      deallocation functions
      (function)[edit]
      obtains the current new handler
      (function)[edit]
      registers a new handler
      (function)[edit]
      (deprecated in C++17)(removed in C++20)
      obtains uninitialized storage
      (function template)[edit]
      allocates memory
      (function)[edit]
      allocates aligned memory
      (function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/memory/new/operator_new&oldid=181162"

      [8]ページ先頭

      ©2009-2025 Movatter.jp