pastebin - collaborative debugging tool
rovema.kpaste.net RSS

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+
12
/*
23
 * c++pmr1.cpp - Simple C++17 PMR example+out-of-memory condition
34
 *
45
 * Compile and run with
56
 * ---- snip ----
67
 * 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
148
 * ---- snip ----
9+
 * OR (Cygwin 3.6 with gcc 15.0):
1513
 *
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'
2327
28+
#include <array>
2432
33+
2534
int main(int ac, char *av[])
2635
{
27-
	char buffer[256];
36+
	char buffer[4096];
2837
2938
	/*
3039
	 * Note that without |std::pmr::null_memory_resource()| |monotonic_buffer_resource()| will
3140
	 * use the default memory resource to obtain memory if it runs out of memory
3241
	 */
3342
	std::pmr::monotonic_buffer_resource pool{buffer, sizeof(buffer), std::pmr::null_memory_resource() };
3443
	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};
3648
3749
	try {
38-
		for (int i=0 ; i < 12 ; i++) {
50+
		for (int i=0 ; i < 8 ; i++) {
3951
			std::cout << "# Cycle: " << i << ": str='" << s << "'" << std::endl;
40-
			s+=s;
52+
			s2 = s;
53+
			s3 = s;
4156
		}
4257
4358
		std::cout << s << std::endl;
4459
	} catch(const std::exception &e) {
4560
		std::cerr << "DIE!!" << e.what() << std::endl;
4661
	}
4762
4863
	return EXIT_SUCCESS;
4964
}
5065

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