allocator

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

时间秒杀一切 提交于 2019-12-30 08:23:15
问题 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

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

徘徊边缘 提交于 2019-12-30 08:15:30
问题 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

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

为君一笑 提交于 2019-12-30 08:12:33
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 6 years ago . 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

Unable to use custom allocator with allocate_shared/make_shared

邮差的信 提交于 2019-12-30 06:17:10
问题 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

Why is there an error message when splicing list elements from lists with different allocators? And how can this be fixed?

左心房为你撑大大i 提交于 2019-12-24 19:23:46
问题 Hello I'm trying to transfer/move an element from one list (in the example foo) at (end()-1) to another list (in the example called bar) to position begin(). The only problem is that one of the lists is using an custom made allocator. Which probably results to the following error message: ../src/TestAllocator.cpp:120:28: error: no matching function for call to ‘std::list<int>::splice ( std::_List_iterator<int>&, std::list<int, CustomAllocator<int> >&, std::_List_iterator<int>&)’ My Question

std::regex_match with another Allocator

倖福魔咒の 提交于 2019-12-24 17:17:39
问题 I'm stuck how to use the regex_match template with my own Memory STL Allocator. This is my code: FaF::smatch stringResults; std::regex expression( "expression" ); std::regex_match( FaF::string-variable, stringResults, expression ); For std::match and std::string I was successful and therefore I use it in the above example: namespace FaF { using smatch = std::match_results<std::string::const_iterator, Allocator<std::string::const_iterator>>; using string = std::basic_string<char, std::char

C++ stateful allocator de-allocate issues

喜夏-厌秋 提交于 2019-12-24 06:42:12
问题 This issue is my misunderstanding of how the standard is using my custom allocator. I have a stateful allocator that keeps a vector of allocated blocks. This vector is pushed into when allocating and searched through during de-allocation. From my debugging it appears that different instances of my object (this*'s differ) are being called on de-allocation. An example may be that MyAllocator (this* = 1) is called to allocate 20 bytes, then some time later MyAllocator (this* = 2) is called to de

is uninitialized_copy/fill(In first, In last, For dest, A &a) an oversight in the c++ standard?

本小妞迷上赌 提交于 2019-12-23 21:15:34
问题 I like to know how things work and as such have been delving deep into the c++ standard library. Something occurred to me the other day. It is required that containters (for example: std::vector<int, std::allocator<int> > ) use the allocator specified for allocations. Specifically the standard says: 23.1.8 Copy constructors for all container types defined in this clause copy an allocator argument from their respective first parameters. All other constructors for these container types take an

C++ Design Pattern for allocator type arguments

那年仲夏 提交于 2019-12-23 10:57:30
问题 The C++03 standard library uses simple template type arguments when passing a type to a class which is meant to be an allocator. This is possible because of how templates work in C++. However, it isn't very straightforward and you might don't know what exactly the type definition should look like - especially in case of non standard types. I thought it might be a good idea to use adaptor classes instread. I've created an example to show you what I mean: #ifndef HPP_ALLOCATOR_ADAPTOR_INCLUDED

How to create a C++ 11 non-default-constructible allocator?

浪尽此生 提交于 2019-12-23 09:55:51
问题 This subject came up in this thread about a change to std::list::sort() for Visual Studio 2015: `std::list<>::sort()` - why the sudden switch to top-down strategy? The new version of std::list::sort does not require a default constructible std::list, as it only uses iterators, and doesn't create any local lists, so it doesn't matter if lists can't be default constructed. The prior version uses local lists (note - each instance of a list involves a dynamic allocation of a sentinel node):