allocator

C++ allocators, specifically passing constructor arguments to objects allocated with boost::interprocess::cached_adaptive_pool

北城余情 提交于 2019-12-23 08:59:40
问题 This is an embarrassing question, but even the well-written documentation provided with boost.interprocess hasn't been enough for me to figure out how to do this. What I have is a cached_adaptive_pool allocator instance, and I want to use it to construct an object, passing along constructor parameters: struct Test { Test(float argument, bool flag); Test(); }; // Normal construction Test obj(10, true); // Normal dynamic allocation Test* obj2 = new Test(20, false); typedef managed_unique_ptr<

Custom STL Allocator with a custom constructor

人走茶凉 提交于 2019-12-23 03:33:15
问题 I'm using the STL allocator mentioned here. The only change I'm making is that I'm inheriting from a base class called Object, and I use base class' new and delete functions for allocation. class MyAlloc :public Object{ ...... } I want to use the parameterized constructor of the base class which will be based on parameter sent to the STLAllocator, which would be something like this. MyAlloc(A *a) : Object(a) { ... } And then use this constructor like : A *a = new A(); std::vector<int,MyAlloc

Eigen library memory usage for dynamic vectors

故事扮演 提交于 2019-12-23 03:02:03
问题 I have a binary file storing float32 objects (9748422*5 of them). From such a collection (190MB roughly in size), I'm creating a set of Eigen::VectorXd vectors (each with 5 components), thus 9748422 of them. The underlying type is double , hence roughly double the input size for storing them. But, as luck has it, the process requires a total of 2.5GB. This is a log of the PROCESS_MEMORY_COUNTERS : PageFaultCount: 0x000A3C40 PeakWorkingSetSize: 0xA3C42000 WorkingSetSize: 0xA3C42000

How to exactly simulate new T[n] with an allocator?

醉酒当歌 提交于 2019-12-23 01:16:15
问题 The expression new T[n] may or may not initialize each object in the array, depending on what T is. How do I replicate this initialization behavior using an allocator ? struct Foo { int x; Foo() : x(1) { } }; Foo *p = new Foo[1]; assert(p[0].x == 1); 回答1: In C++03, the allocator interface only knows one way to initialize objects, and that's to copy from another object. C++11 has more. You're asking for default initialization, which means (approximately), "either do nothing or call the default

Does an allocator.construct loop equal std::uninitialized_copy?

谁都会走 提交于 2019-12-22 05:13:47
问题 In this context T is a certain type and allocator is an allocator object for that type. By default it is std::allocator<T> but this is not necessarily true. I have a chunk of memory acquired by allocator.allocate(n) . I also have a container con of T objects (say, a std::vector<T> ). I want to initialize that chunk of memory with the T object(s). The location of the chunk of memory is stored in T* data . Are these two code examples always identical? #include <memory> // example 1 std:

C++ allocator<X>::deallocate(NULL,1) allowed?

孤街醉人 提交于 2019-12-22 04:45:12
问题 Both free(NULL) and ::operator delete(NULL) are allowed. Does the allocator concept (e.g. std::allocator also allow deallocate(NULL,1) , or is it required to put your own guard around it? 回答1: You'll need to add your own check. According to §20.4.​1.1/8, deallocate requires: p shall be a pointer value obtained from allocate(). n shall equal the value passed as the first argument to the invocation of allocate which returned p. allocate throws an exception when storage can't be given (§20.4.​1

Is libstdc++ support for std::unordered_map incomplete?

旧街凉风 提交于 2019-12-22 04:39:18
问题 Related to this question on CodeReview, I tried to use std::unordered_map with a custom allocator but apparently this does not work with gcc/clang and libstdc++. The error can be generated from initializing an empty hash map with a std::allocator #include <unordered_map> int main() { typedef std::allocator<std::pair<const int, int>> A; typedef std::unordered_map<int, int, std::hash<int>, std::equal_to<int>, A> H; auto h = H{A()}; // ERROR, cannot find constructor H::H(const A&) } Live Example

Difference between allocator supplied as template parameter and allocator supplied as constructor argument in C++ containers?

拟墨画扇 提交于 2019-12-21 20:42:36
问题 What's the difference between supplying an STL container (for example, std::vector) with an allocator as a template parameter, eg.: std::vector<int, std::allocator<int>> some_ints; and supplying an allocator as a constructor argument, eg: std::allocator<int> temp; std::vector<int> some_ints(temp); and what are the advantages of either, given that they are not the same thing (ie. one supplies a type, the other a type instance) and can be used separately from each other? 回答1: Can be used

lock free arena allocator implementation - correct?

廉价感情. 提交于 2019-12-21 17:33:34
问题 for a simple pointer-increment allocator (do they have an official name?) I am looking for a lock-free algorithm. It seems trivial, but I'd like to get soem feedback whether my implementaiton is correct. not threadsafe implementation: byte * head; // current head of remaining buffer byte * end; // end of remaining buffer void * Alloc(size_t size) { if (end-head < size) return 0; // allocation failure void * result = head; head += size; return head; } My attempt at a thread safe implementation

mismatched std::allocator for some of STL containers

不打扰是莪最后的温柔 提交于 2019-12-21 09:17:05
问题 Is it technically valid to use mismatched std::allocator specialization (surely, except its specialization for void ) as a template parameter for STL containers (not all of them, but enumerated below plus unordered_(multi)map/set)? Following code compiles fine. #include <list> #include <forward_list> #include <deque> #include <set> #include <map> int main() { struct A { bool operator < (A) const { return true; } }; struct B {}; struct C {}; std::list< A, std::allocator< C > > l; std::forward