I introduced in my last post “Polymorphic Allocators with C++17” the theory of polymorphic allocators in C++17. Today, I will apply the theory.
Before I go on, here are the essential parts of my last post: “Polymorphic Allocators with C++17“.
A Short Reminder
The following program uses polymorphic allocators.
Now, I want to focus on my
Vec2. It pushes 200
ints onto the
ints do not fit into a
char buf and, therefore,
std::pmr::new_delete_resource() as the so-called upstream allocator kicks in and calls the global
new for the remaining elements. Let me instrumentalize the upstream allocator.
A Tracking Allocator
The following program is based on the previous one, uses a tracking allocator, and makes the dynamic memory allocation and deallocation visible.
TrackAllocator is a tracking allocator. It derives from the interface class
std::pmr::memory_resource, from which all memory resources derive.
TrackAllocator defines the three required member functions
do_is_equal. Each call forwards its call to
std:pmr::new_delete_resource is the default memory resource and calls the global
delete. The member function’s job
do_is_equal is to
By instantiating the
TrackAllocator (line 1) and make it as the default resource (line 2).
myVec1 (line 3) and
myVec2 (line 4) use them as the upstream allocator. This allocator kicks in if the primary allocator is consumed. This fallback is not necessary for
myVec1, but necessary for
This output shows the dynamical allocation and deallocation of
myVec2 until all
ints fit into the
You can also bind an upstream allocator to a specific container.
A Non-Allocating Allocator
std::pmr::null_resource_allocator is a special allocator. Using this memory resource for allocating causes a
std::bad_alloc exception. This memory resource ensures that you do not arbitrarily allocate memory on the heap. Let’s try it out.
First, I allocate memory on the stack and initialize
std::pmr::monotonic_buffer_resource with it. Then, I use this memory resource and
std::pmr::null_memory_resource as an upstream allocator (line 1). In line 2, I create a
std::pmr::vector<std::pmr::string>. Because I use a
std::pmr::string, the string also uses the memory resource and its upstream allocator. When I use
std::string allocates using the global
delete. Finally, in line (3), I create 100 strings and catch in line (4) a
std::bad_alloc exception. I got what I deserved. 100 strings will not fit into a
std::array<std::byte, 2000> buffer. After 17 strings, the buffer is consumed.
You may have heard it already. The
std::pmr::monotonic_buffer has outstanding features. It is pretty fast and does not free memory. I will provide the number in my next post.
Thanks a lot to my Patreon Supporters: Matt Braun, Roman Postanciuc, Tobias Zindl, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Jozo Leko, John Breland, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Daniel Hufschläger, Alessandro Pezzato, Bob Perry, Satish Vangipuram, Andi Ireland, Richard Ohnemus, Michael Dunsky, Leo Goodstadt, John Wiederhirn, Yacob Cohen-Arazi, Florian Tischler, Robin Furness, Michael Young, Holger Detering, Bernd Mühlhaus, Matthieu Bolt, Stephen Kelley, Kyle Dean, Tusar Palauri, Dmitry Farberov, Juan Dent, George Liao, Daniel Ceperley, Jon T Hess, Stephen Totten, Wolfgang Fütterer, Matthias Grün, Phillip Diekmann, Ben Atakora, Ann Shatoff, Rob North, Bhavith C Achar, Marco Parri Empoli, moon, and Philipp Lenk.
Thanks, in particular, to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, John Nebel, Mipko, Alicja Kaminska, Slavko Radman, and David Poole.
|My special thanks to Embarcadero|
|My special thanks to PVS-Studio|
|My special thanks to Tipi.build|
|My special thanks to Take Up Code|
|My special thanks to SHAVEDYAKS|
I’m happy to give online seminars or face-to-face seminars worldwide. Please call me if you have any questions.
- Embedded Programmierung mit modernem C++ 12.12.2023 – 14.12.2023 (Präsenzschulung, Termingarantie)
Standard Seminars (English/German)
Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.
- C++ – The Core Language
- C++ – The Standard Library
- C++ – Compact
- C++11 and C++14
- Concurrency with Modern C++
- Design Pattern and Architectural Pattern with C++
- Embedded Programming with Modern C++
- Generic Programming (Templates) with C++
- Clean Code with Modern C++
- Phone: +49 7472 917441
- Mobil:: +49 176 5506 5086
- Mail: schulung@ModernesCpp.de
- German Seminar Page: www.ModernesCpp.de
- Mentoring Page: www.ModernesCpp.org
Modernes C++ Mentoring,