pastebin - collaborative debugging tool
rovema.kpaste.net RSS


c++pmr1.cpp PMR buffer too small test 1
Posted by Anonymous on Sat 6th Jul 2024 14:55
raw | new post
view followups (newest first): c++pmr1.cpp PMR buffer too small test 1 by Anonymous
modification of post by Anonymous (view diff)

  1. /*
  2.  * c++pmr1.cpp - Simple C++17 PMR example+out-of-memory condition
  3.  *
  4.  * Compile and run with
  5.  * ---- snip ----
  6.  * rm -f a.exe* ; clang++ -target x86_64-w64-mingw32 -std=c++17 -Bstatic c++pmr1.cpp
  7.  * $ PATH+=':/usr/x86_64-w64-mingw32/sys-root/mingw/bin/' ./a.exe
  8.  * # Cycle: 0: str='x'
  9.  * # Cycle: 1: str='xx'
  10.  * # Cycle: 2: str='xxxx'
  11.  * # Cycle: 3: str='xxxxxxxx'
  12.  * # Cycle: 4: str='xxxxxxxxxxxxxxxx'
  13.  * DIE!!std::bad_alloc
  14.  * ---- snip ----
  15.  *
  16.  * Written by Roland Mainz <roland.mainz@nrubsig.org>
  17.  */
  18.  
  19. #include <array>
  20. #include <cstddef>
  21. #include <iostream>
  22. #include <memory_resource>
  23.  
  24.  
  25. int main(int ac, char *av[])
  26. {
  27.         char buffer[256];
  28.  
  29.         /*
  30.          * Note that without |std::pmr::null_memory_resource()| |monotonic_buffer_resource()| will
  31.          * use the default memory resource to obtain memory if it runs out of memory
  32.          */
  33.         std::pmr::monotonic_buffer_resource pool{buffer, sizeof(buffer), std::pmr::null_memory_resource() };
  34.         std::pmr::polymorphic_allocator<char> pa{&pool};
  35.         std::pmr::string s{"x", pa};
  36.  
  37.         try {
  38.                 for (int i=0 ; i < 12 ; i++) {
  39.                         std::cout << "# Cycle: " << i << ": str='" << s << "'" << std::endl;
  40.                         s+=s;
  41.                 }
  42.  
  43.                 std::cout << s << std::endl;
  44.         } catch(const std::exception &e) {
  45.                 std::cerr << "DIE!!" << e.what() << std::endl;
  46.         }
  47.  
  48.         return EXIT_SUCCESS;
  49. }

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at