- /*
- * 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
- * $ 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[256];
- /*
- * 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{"x", pa};
- try {
- for (int i=0 ; i < 12 ; i++) {
- std::cout << "# Cycle: " << i << ": str='" << s << "'" << std::endl;
- s+=s;
- }
- 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 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)
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.