Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::pmr::set_default_resource

      From cppreference.com
      <cpp‎ |memory
       
       
      Memory management library
      (exposition only*)
      Allocators
      Uninitialized memory algorithms
      Constrained uninitialized memory algorithms
      Memory resources
      pmr::set_default_resource
      (C++17)
      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<memory_resource>
      std::pmr::memory_resource* set_default_resource(std::pmr::memory_resource* r)noexcept;
      (since C++17)

      Ifr is not null, sets the default memory resource pointer tor; otherwise, sets the default memory resource pointer tostd::pmr::new_delete_resource().

      Thedefault memory resource pointer is used by certain facilities when an explicit memory resource is not supplied. The initial default memory resource pointer is the return value ofstd::pmr::new_delete_resource.

      This function is thread-safe. Every call tostd::pmr::set_default_resourcesynchronizes with (seestd::memory_order) the subsequentstd::pmr::set_default_resource andstd::pmr::get_default_resource calls.

      [edit]Return value

      Returns the previous value of the default memory resource pointer.

      [edit]Example

      Run this code
      #include <array>#include <cstddef>#include <cstdint>#include <iostream>#include <iterator>#include <memory_resource>#include <vector> class noisy_allocator:publicstd::pmr::memory_resource{void* do_allocate(std::size_t bytes,std::size_t alignment) override{std::cout<<"+ Allocating "<< bytes<<" bytes @ ";void* p=std::pmr::new_delete_resource()->allocate(bytes, alignment);std::cout<< p<<'\n';return p;} void do_deallocate(void* p,std::size_t bytes,std::size_t alignment) override{std::cout<<"- Deallocating "<< bytes<<" bytes @ "<< p<<'\n';returnstd::pmr::new_delete_resource()->deallocate(p, bytes, alignment);} bool do_is_equal(conststd::pmr::memory_resource& other)constnoexcept override{returnstd::pmr::new_delete_resource()->is_equal(other);}}; int main(){constexprint push_back_limit{16};    noisy_allocator mem;    std::pmr::set_default_resource(&mem); {std::cout<<"Entering scope #1 (without buffer on stack)...\n";std::cout<<"Creating vector v...\n";std::pmr::vector<std::uint16_t> v{1,2,3,4};std::cout<<"v.data() @ "<< v.data()<<'\n'; std::cout<<"Requesting more...\n";for(int i{0}; i!= push_back_limit;++i){            v.push_back(i);std::cout<<"v.size(): "<< v.size()<<'\n';}std::cout<<"Exiting scope #1...\n";} std::cout<<'\n'; {std::cout<<"Entering scope #2 (with buffer on stack)...\n"; std::uint8_t buffer[16];std::cout<<"Allocating buffer on stack: "<< sizeof buffer<<" bytes @ "<<static_cast<void*>(buffer)<<'\n';std::pmr::monotonic_buffer_resource mem_res{std::data(buffer),std::size(buffer)}; std::cout<<"Creating vector v...\n";std::pmr::vector<std::uint16_t> v{{1,2,3,4},&mem_res};std::cout<<"v.data() @ "<< v.data()<<'\n';// equals to `buffer` address std::cout<<"Requesting more...\n";for(int i{0}; i!= push_back_limit;++i){            v.push_back(i);std::cout<<"v.size(): "<< v.size()<<'\n';}std::cout<<"Exiting scope #2...\n";}}

      Possible output:

      Entering scope #1 (without buffer on stack)...Creating vector v...+ Allocating 8 bytes @ 0x1f75c30v.data() @ 0x1f75c30Requesting more...+ Allocating 16 bytes @ 0x1f75c50- Deallocating 8 bytes @ 0x1f75c30v.size(): 5v.size(): 6v.size(): 7v.size(): 8+ Allocating 32 bytes @ 0x1f75c70- Deallocating 16 bytes @ 0x1f75c50v.size(): 9v.size(): 10v.size(): 11v.size(): 12v.size(): 13v.size(): 14v.size(): 15v.size(): 16+ Allocating 64 bytes @ 0x1f75ca0- Deallocating 32 bytes @ 0x1f75c70v.size(): 17v.size(): 18v.size(): 19v.size(): 20Exiting scope #1...- Deallocating 64 bytes @ 0x1f75ca0 Entering scope #2 (with buffer on stack)...Allocating buffer on stack: 16 bytes @ 0x7fffbe9f8240Creating vector v...v.data() @ 0x7fffbe9f8240Requesting more...+ Allocating 64 bytes @ 0x1f75ca0v.size(): 5v.size(): 6v.size(): 7v.size(): 8v.size(): 9v.size(): 10v.size(): 11v.size(): 12v.size(): 13v.size(): 14v.size(): 15v.size(): 16+ Allocating 128 bytes @ 0x1f75cf0v.size(): 17v.size(): 18v.size(): 19v.size(): 20Exiting scope #2...- Deallocating 128 bytes @ 0x1f75cf0- Deallocating 64 bytes @ 0x1f75ca0

      [edit]See also

      gets the defaultstd::pmr::memory_resource
      (function)[edit]
      returns a static program-widestd::pmr::memory_resource that uses the globaloperator new andoperator delete to allocate and deallocate memory
      (function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/memory/set_default_resource&oldid=160465"

      [8]ページ先頭

      ©2009-2025 Movatter.jp