fenbf / cppstories-discussions

4 stars 1 forks source link

2020/06/pmr-hacking.html/ #62

Open utterances-bot opened 2 years ago

utterances-bot commented 2 years ago

Polymorphic Allocators, std::vector Growth and Hacking - C++ Stories

The concept of a polymorphic allocator from C++17 is an enhancement to standard allocators from the Standard Library. It’s much easier to use than a regular allocator and allows containers to have the same type while having a different allocator, or even a possibility to change allocators at runtime. Let’s see how we can use it and hack to see the growth of std::vector containers.

https://www.cppstories.com/2020/06/pmr-hacking.html/

getsoubl commented 2 years ago

Is any rule for setting options in pool_resource pmr to avoid unnecessary memory allocations? Find my comment in stack overflow https://stackoverflow.com/questions/69629967/c17-pmr-set-number-of-blocks-and-their-size-in-a-unsynchronized-pool-resourc

tmcopeland commented 2 years ago

I came here in hopes of finding tips about implementing memory_resource::do_is_equal. How can one assure that two polymorphic allocators (abstract classes) have access to the same memory? True polymorphism in C++ is throwing me for a loop, since I've developed an affinity for templates.

rdmeneze commented 1 year ago

I'd like to understand why on the first example, even the loop inserts the characters in a ordered way, on the vector we have repeated values...

fenbf commented 1 year ago

Hi @rdmeneze the vector grows, and then is has to reallocate memory. So in the first stage is only uses one memory cell, then it grows and allocates two cells, and copies the first element and puts the second one... and so on.