- /*
- * c++pmr1.cpp - Simple C++17 PMR example+out-of-memory condition
- *
- * Compile and run with
- * ---- snip ----
- * rm -f a.exe* ; clang++ -target x86_64-w64-mingw32 -std=c++17 -Bstatic c++pmr1.cpp
- * ---- snip ----
- * OR (Cygwin 3.6 with gcc 15.0):
- * ---- snip ----
- * rm -f a.exe* ; g++ -std=c++20 -Wall -Wextra -D_GLIBCXX_USE_CXX11_ABI c++pmr1.cpp
- * ---- snip ----
- *
- * Output:
- * ---- snip ----
- * $ PATH+=':/usr/x86_64-w64-mingw32/sys-root/mingw/bin/' ./a.exe
- * # Cycle: 0: str='x'
- * # Cycle: 1: str='xx'
- * # Cycle: 2: str='xxxx'
- * # Cycle: 3: str='xxxxxxxx'
- * # Cycle: 4: str='xxxxxxxxxxxxxxxx'
- * DIE!!std::bad_alloc
- * ---- snip ----
- *
- * Written by Roland Mainz <roland.mainz@nrubsig.org>
- */
- #include <array>
- #include <cstddef>
- #include <iostream>
- #include <memory_resource>
- int main(int ac, char *av[])
- {
- char buffer[4096];
- /*
- * Note that without |std::pmr::null_memory_resource()| |monotonic_buffer_resource()| will
- * use the default memory resource to obtain memory if it runs out of memory
- */
- std::pmr::monotonic_buffer_resource pool{buffer, sizeof(buffer), std::pmr::null_memory_resource() };
- std::pmr::polymorphic_allocator<char> pa{&pool};
- std::pmr::string s{"12", pa};
- std::pmr::string s2{pa};
- std::pmr::string s3{pa};
- std::pmr::string s4{pa};
- try {
- for (int i=0 ; i < 8 ; i++) {
- std::cout << "# Cycle: " << i << ": str='" << s << "'" << std::endl;
- s2 = s;
- s3 = s;
- s4 = s2 + s3;
- s = s4;
- }
- std::cout << s << std::endl;
- } catch(const std::exception &e) {
- std::cerr << "DIE!!" << e.what() << std::endl;
- }
- return EXIT_SUCCESS;
- }
c++pmr1.cpp PMR buffer too small test 1
Posted by Anonymous on Mon 18th Nov 2024 08:48
raw | new post
modification of post by Anonymous (view diff)
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.