allocator

Why allow `propagate_on_container_swap == false` in Allocators, when it might cause undefined behaviour?

牧云@^-^@ 提交于 2019-12-03 06:04:40
Note: Originally asked by Matt Mcnabb as a comment on Why can swapping standard library containers be problematic in C++11 (involving allocators)? . The Standard ( N3797 ) says that if progagate_on_container_swap inside an Allocator is std::false_type it will yield undefined behaviour if the two allocators involved doesn't compare equal. Why would the Standard allow such construct when it seems more than dangerous? 23.2.1p9 General Container Requirements [container.requirements.general] If allocator_traits<allocator_type>::propagate_on_container_swap::value is true , then the allocators of a

How to use boost::pool library to create a custom memory allocator

蓝咒 提交于 2019-12-03 00:32:56
I am new to boost and I want to know how exactly the boost::pool libraries can help me in creating a custom memory allocator. And I have two vector of struct objects. First vector is of structure type A, while second vector is of structure type B. How can I reuse the memory allocated to the first vector to the second vector. Boost Pool is a library that defines a few allocator types. Obviously, the focus of the library is to provide Pool Allocators. Pool Allocators shine when you allocate objects of identical size. Note If your structure A and structure B aren't identical/very similar size you

Setting a custom allocator for strings

北城以北 提交于 2019-12-03 00:04:26
I know I can set a custom allocator for vectors using the syntax vector<T, Alloc> . Is there a way I can do the same for strings? Yes. All string classes come from the class template basic_string , declared as such: template <class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> > class basic_string; For example, std::string is just typedef basic_string<char> string; . The third template parameter is the allocator, so you can do something like: typedef basic_string<char, char_traits<char>, my_allocator<char> > my_string; 来源: https://stackoverflow.com/questions

STL Container: Constructor's Allocator parameter and scoped allocators

社会主义新天地 提交于 2019-12-02 19:24:05
There is a template parameter for STL containers to chose a custom allocator. It took a while, but I think I understand how it works. Somehow it isn't really nice because the given allocator type isn't used directly but it is rebound to the allocator of another type. Finally I can work with it. After reading the API I recognized that there is also the possibility to give allocators as constructor parameter. But how do I know which kind of allocator the container uses, if it internally rebinds the given allocator from the template parameter? Additionally I read that C++11 now uses scoped

Custom (pool) allocator with boost shared_ptr

杀马特。学长 韩版系。学妹 提交于 2019-12-02 17:09:22
I want objects managed by a shared_ptr to be allocated from a pool, say Boost's Pool interface, how can this be achieved? Here's the code to do what you want (probably won't compile as I don't have boost on hand and I'm writing it from memory): class YourClass; // your data type, defined somewhere else boost::object_pool<YourClass> allocator; void destroy(YourClass* pointer) { allocator.destroy(pointer); } boost::shared_ptr<YourClass> create() { // usage of object_pool<??>::construct requires that you have a // YourClass::YourClass(void) defined. If you need to pass arguments // to the new

C++ STL allocator vs operator new

☆樱花仙子☆ 提交于 2019-12-02 15:38:22
According to C++ Primer 4th edition, page 755, there is a note saying: Modern C++ programs ordinarily ought to use the allocator class to allocate memory. It is safer and more flexible. I don't quite understand this statement. So far all the materials I read teach using new to allocate memory in C++. An example of how vector class utilize allocator is shown in the book. However, I cannot think of other scenarios. Can anyone help to clarify this statement? and give me more examples? When should I use allocator and when to use new ? Thanks! For general programming, yes you should use new and

Is it still possible to customize STL vector's “reference” type?

和自甴很熟 提交于 2019-12-01 11:27:34
Is it possible to customize the reference of a std::vector . Until C++11 it seemed to be possible through the Allocator template parameter. But not anymore? According to the documentation, http://en.cppreference.com/w/cpp/container/vector , reference is now always value_type and value_type is always the template parameter T . It seems to be impossible even using allocator_traits , http://en.cppreference.com/w/cpp/memory/allocator_traits Is there a workaround for this? If not, does it means that I have to specialize the entire std::vector and probably reproduce all its functionality if I want

Is it still possible to customize STL vector's “reference” type?

ε祈祈猫儿з 提交于 2019-12-01 08:02:12
问题 Is it possible to customize the reference of a std::vector . Until C++11 it seemed to be possible through the Allocator template parameter. But not anymore? According to the documentation, http://en.cppreference.com/w/cpp/container/vector, reference is now always value_type and value_type is always the template parameter T . It seems to be impossible even using allocator_traits , http://en.cppreference.com/w/cpp/memory/allocator_traits Is there a workaround for this? If not, does it means

std::unique_ptr<T[]> and custom allocator deleter

被刻印的时光 ゝ 提交于 2019-12-01 03:44:54
I am trying to use std::unique_ptr<T[]> with custom memory allocators. Basically, I have custom allocators that are subclasses of IAllocator , which provides the following methods: void* Alloc( size_t size ) template<typename T> T* AllocArray( size_t count ) void Free( void* mem ) template<typename T> void FreeArray( T* arr, size_t count ) Since the underlying memory might come from a pre-allocated block, I need the special ...Array() -methods to allocate and free arrays, they allocate/free memory and call T() / ~T() on every element in the range. Now, as far as I know, custom deleters for std

What are allocators and when is their use necessary? [closed]

佐手、 提交于 2019-12-01 03:14:37
While reading books on C++ and the standard library, I see frequent references to allocators. For example, Nicolai Josuttis's The C++ Standard Library discusses them in detail in the last chapter, and both items 10 ("be aware of allocators' conventions & restrictions") and 11 ("understand the legitimate uses of custom allocators") in Scott Meyers's Effective STL are about their use. My question is, how do allocators represent a special memory model? Is the default STL memory management not enough? When should allocators be used instead? If possible, please explain with a simple memory model