allocator

Custom allocator for std::vector<char> is ignored

丶灬走出姿态 提交于 2019-12-01 03:14:16
I was trying to use a custom allocator for std::vector<char> , but I noticed that std::vector does not need/use any of the member functions from my allocator. How is this possible? #include <vector> struct A : private std::allocator<char> { typedef std::allocator<char> alloc; using alloc::value_type; using alloc::pointer; using alloc::const_pointer; using alloc::difference_type; using alloc::size_type; using alloc::rebind; // member functions have been removed, since the program compiles without them }; int main() { std::vector<char, A> v; v.resize(4000); for (auto& c : v) if (c) return 1; //

Unable to use custom allocator with allocate_shared/make_shared

房东的猫 提交于 2019-11-30 19:17:15
In my C++11 program, I use shared_ptr<T> for some objects which are actively created and deleted. It so happened that standard allocator with operator new is a bottleneck, so I want to create my own one, which will allocate a bunch of memory at once and then give to to make_shared on demand. Unfortunatelly, this is the first time I write an allocator and I have no idea why GCC is unable to compile the following code: #include <memory> class MyAlloc { public: typedef char* pointer; typedef const char* const_pointer; typedef char value_type; char* allocate(size_t len) { return new char[len]; }

Is there an allocator that uses alloca and is otherwise C++ STL compliant?

别来无恙 提交于 2019-11-30 14:58:29
问题 I have two questions: 1) Is it possible to implement an allocator that uses alloca to allocate memory on the stack and is otherwise C++ STL compliant? If there is code out there, you can make me happy by simply pointing me to the URL. :-) If there is no code out there, perhaps you can sketch the functions allocate and deallocate? 2) If the answer to the above question is 'yes', I'd like to understand how it is possible to allocate memory on the stack for class members. As an example, consider

Standard library facilities which allocate but don't use an Allocator

狂风中的少年 提交于 2019-11-30 12:56:47
In most places where the C++ standard library allocates memory, the user is able to customise this by providing a class which meets the Allocator requirements . For example, almost all containers take an allocator template argument, and std::allocate_shared returns a shared_ptr whose contained element and control block are both allocated via a provided Allocator. However, there are a few places where the standard library can (potentially) allocate memory, but no Allocator support is provided. The ones I can think of are: std::make_unique() (no corresponding allocate_unique() ) std::any std:

Is there an allocator that uses alloca and is otherwise C++ STL compliant?

我怕爱的太早我们不能终老 提交于 2019-11-30 12:36:52
I have two questions: 1) Is it possible to implement an allocator that uses alloca to allocate memory on the stack and is otherwise C++ STL compliant? If there is code out there, you can make me happy by simply pointing me to the URL. :-) If there is no code out there, perhaps you can sketch the functions allocate and deallocate? 2) If the answer to the above question is 'yes', I'd like to understand how it is possible to allocate memory on the stack for class members. As an example, consider an std::vector<int, AllocaAllocator<int> > and suppose that a call of the member function 'resize' of

Custom allocator in std::vector

让人想犯罪 __ 提交于 2019-11-30 08:05:10
Is it possible to use custom allocator for std::vector internal allocations? If yes, how? You basically have to implement your allocator type to conform to the Allocator concept . The linked page lists all requirements of that type, but the core functionality is implemented in the allocate member function. 来源: https://stackoverflow.com/questions/11896960/custom-allocator-in-stdvector

What is the purpose of std::scoped_allocator_adaptor?

不打扰是莪最后的温柔 提交于 2019-11-30 00:03:50
In the C++11 standard we have std::scoped_allocator_adaptor in the dynamic memory management library. What are the most important use cases of this class? If you want a container of strings and want to use the same allocator for the container and its elements (so they are all allocated in the same arena, as TemplateRex describes) then you can do that manually: template<typename T> using Allocator = SomeFancyAllocator<T>; using String = std::basic_string<char, std::char_traits<char>, Allocator<char>>; using Vector = std::vector<String, Allocator<String>>; Allocator<String> as( some_memory

How to make my uninitialised_allocator safe?

怎甘沉沦 提交于 2019-11-29 11:09:28
Following from this question , I want to use an unitialised_allocator with, say, std::vector to avoid default initialisation of elements upon construction (or resize() of the std::vector (see also here for a use case). My current design looks like this: // based on a design by Jared Hoberock template<typename T, typename base_allocator > struct uninitialised_allocator : base_allocator::template rebind<T>::other { // added by Walter Q: IS THIS THE CORRECT CONDITION? static_assert(std::is_trivially_default_constructible<T>::value, "value type must be default constructible"); // added by Walter Q

Custom allocator in std::vector

ぃ、小莉子 提交于 2019-11-29 05:30:19
问题 Is it possible to use custom allocator for std::vector internal allocations? If yes, how? 回答1: You basically have to implement your allocator type to conform to the Allocator concept. The linked page lists all requirements of that type, but the core functionality is implemented in the allocate member function. 来源: https://stackoverflow.com/questions/11896960/custom-allocator-in-stdvector

Does C++11 require allocators to be default constructible, libstdc++ and libc++ disagree?

萝らか妹 提交于 2019-11-28 23:46:28
Using a slightly modified version of Howard Hinnants's C++11 stack allocator which is documented here and here , with std::basic_string and compiling with gcc which is using libstdc++ , the following example ( see it live ): const unsigned int N = 200; arena<N> a; short_alloc<char, N> ac(a) ; std::basic_string<char,std::char_traits<char>,short_alloc<char, N>> empty(ac); gives the following error( amongst others ): error: no matching function for call to 'short_alloc<char, 200ul>::short_alloc()' if (__n == 0 && __a == _Alloc()) ^ However it works without error when compiling with clang and