Difference between
modified post c341bdde6 by Anonymous on Mon 18th Nov 2024 08:48
original post 46b07697 by Anonymous on Sat 6th Jul 2024 14:55
Download diff
Show
old version |
new version |
both versions
| 1 | + | ||
| 1 | 2 | /* | |
| 2 | 3 | * c++pmr1.cpp - Simple C++17 PMR example+out-of-memory condition | |
| 3 | 4 | * | |
| 4 | 5 | * Compile and run with | |
| 5 | 6 | * ---- snip ---- | |
| 6 | 7 | * 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 | 8 | * ---- snip ---- | |
| 9 | + | * OR (Cygwin 3.6 with gcc 15.0): | |
| 15 | 13 | * | |
| 16 | - | * Written by Roland Mainz <roland.mainz@nrubsig.org> | |
| 14 | + | * Output: | |
| 17 | - | */ | |
| 15 | + | * ---- snip ---- | |
| 18 | - | ||
| 16 | + | * $ PATH+=':/usr/x86_64-w64-mingw32/sys-root/mingw/bin/' ./a.exe | |
| 19 | - | #include <array> | |
| 17 | + | * # Cycle: 0: str='x' | |
| 20 | - | #include <cstddef> | |
| 18 | + | * # Cycle: 1: str='xx' | |
| 21 | - | #include <iostream> | |
| 19 | + | * # Cycle: 2: str='xxxx' | |
| 22 | - | #include <memory_resource> | |
| 20 | + | * # Cycle: 3: str='xxxxxxxx' | |
| 21 | + | * # Cycle: 4: str='xxxxxxxxxxxxxxxx' | |
| 23 | 27 | ||
| 28 | + | #include <array> | |
| 24 | 32 | ||
| 33 | + | ||
| 25 | 34 | int main(int ac, char *av[]) | |
| 26 | 35 | { | |
| 27 | - | char buffer[256]; | |
| 36 | + | char buffer[4096]; | |
| 28 | 37 | ||
| 29 | 38 | /* | |
| 30 | 39 | * Note that without |std::pmr::null_memory_resource()| |monotonic_buffer_resource()| will | |
| 31 | 40 | * use the default memory resource to obtain memory if it runs out of memory | |
| 32 | 41 | */ | |
| 33 | 42 | std::pmr::monotonic_buffer_resource pool{buffer, sizeof(buffer), std::pmr::null_memory_resource() }; | |
| 34 | 43 | std::pmr::polymorphic_allocator<char> pa{&pool}; | |
| 35 | - | std::pmr::string s{"x", pa}; | |
| 44 | + | std::pmr::string s{"12", pa}; | |
| 45 | + | std::pmr::string s2{pa}; | |
| 36 | 48 | ||
| 37 | 49 | try { | |
| 38 | - | for (int i=0 ; i < 12 ; i++) { | |
| 50 | + | for (int i=0 ; i < 8 ; i++) { | |
| 39 | 51 | std::cout << "# Cycle: " << i << ": str='" << s << "'" << std::endl; | |
| 40 | - | s+=s; | |
| 52 | + | s2 = s; | |
| 53 | + | s3 = s; | |
| 41 | 56 | } | |
| 42 | 57 | ||
| 43 | 58 | std::cout << s << std::endl; | |
| 44 | 59 | } catch(const std::exception &e) { | |
| 45 | 60 | std::cerr << "DIE!!" << e.what() << std::endl; | |
| 46 | 61 | } | |
| 47 | 62 | ||
| 48 | 63 | return EXIT_SUCCESS; | |
| 49 | 64 | } | |
| 50 | 65 |
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.
rovema.kpaste.net RSS