allocator

Should allocator construct() default initialize instead of value initializing?

不问归期 提交于 2019-12-21 03:46:37
问题 As a followup to this question, the default allocator ( std::allocator<T> ) is required to implement construct as follows (according to [default.allocator]): template <class U, class... Args> void construct(U* p, Args&&... args); Effects : ::new((void *)p) U(std::forward<Args>(args)...) That is, always value-initialization. The result of this is that std::vector<POD> v(num) , for any pod type, will value-initialize num elements - which is more expensive than default-initializing num elements.

Custom (pool) allocator with boost shared_ptr

大憨熊 提交于 2019-12-20 08:49:21
问题 I want objects managed by a shared_ptr to be allocated from a pool, say Boost's Pool interface, how can this be achieved? 回答1: 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

Why std::vector does not have a release method?

爱⌒轻易说出口 提交于 2019-12-19 09:22:06
问题 I found myself in a situation where I would have liked to have an analog of unique_ptr 's release() for std::vector<> . E.g.: std::vector<int> v(SOME_SIZE); //.. performing operations on v int* data = v.release(); // v.size() is now 0 and the ownership of the internal array is released functionUsingAndInternallyDeletingRowPointer(data); Is there a particular reason why this kind of possibility is not provided? May that impose some constraint on std::vector 's the internal implementation? Or

What is the purpose of std::scoped_allocator_adaptor?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-18 10:41:57
问题 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? 回答1: 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>,

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

半腔热情 提交于 2019-12-18 02:44:06
问题 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()

Can I assume allocators don't hold their memory pool directly (and can therefore be copied)?

a 夏天 提交于 2019-12-17 19:22:12
问题 I'm writing a container and would like to permit the user to use custom allocators, but I can't tell if I should pass allocators around by reference or by value. Is it guaranteed (or at least, a reasonable assumption to make) that an allocator object will not contain its memory pool directly, and hence it would be OK to copy an allocator and expect the memory pools of the allocators to be cross-compatible? Or do I always need to pass allocators by reference? (I have found that passing by

Stack-buffer based STL allocator?

寵の児 提交于 2019-12-17 06:27:28
问题 I was wondering if it practicable to have an C++ standard library compliant allocator that uses a (fixed sized) buffer that lives on the stack. Somehow, it seems this question has not been ask this way yet on SO, although it may have been implicitly answered elsewhere. So basically, it seems , as far as my searches go, that it should be possible to create an allocator that uses a fixed size buffer. Now, on first glance, this should mean that it should also be possible to have an allocator

Memory mapped file storage in stl vector

谁说我不能喝 提交于 2019-12-14 01:06:42
问题 I'm trying to implement custom allocator for storing memory mapped files in the std::vector . Files mapping performed by boost::iostreams::mapped_file Allocator type for file memory mapping: template<typename T> class mmap_allocator { public: typedef T value_type; mmap_allocator(const std::string& filename) : _mmfile(filename) { } T* allocate (size_t n) { return reinterpret_cast<T*>(_mmfile.data()); } void deallocate (T* p, size_t n) { p = nullptr; _mmfile.close(); } private: boost::iostreams

C++ new 与 delete

可紊 提交于 2019-12-13 14:05:21
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> new与delete表达式作了什么 对于new来说 计算所需内存的字节数,然后以此为参数调用标准库的operator new(size_t)函数 在operator new()返回的内存上调用类的适当的构造函数初始化一个对象 将operator new() 返回的指针作为表达式的运算结果 也即 new 把内存分配与对象构造合在一起了 对于delete来说 调用指针所指对象的析够函数释放对象本身 调用标准库的operator delete()函数将指针所指内存返还给系统 也即 delete 把释放对象本身与释放对象所占的内存合在一起 new[] 与 delete[] 行为与上类似 allocator类 很多时候并不需要立即在分配的内存上初始化一个对象,与此同时也希望在释放内存之前不调用对象的析钩函数;allocator类提供了这种机制,其成员: allocator<T> a; /* a可以用来分配原始内存以及在原始内存上构造T类型对象 */ a.allocate(n); /* 分配原始的内存,可以保存n个Type类型的对象 */ a.deallocate(p,n);/* 释放p所指的原始内存,p必须合法;并且不会调用p所指对象的析钩函数,n表示p所指的内存可以保存n个T类型对象 */ a.construct(p

Type erasure and allocators: what's the expected behavior?

左心房为你撑大大i 提交于 2019-12-12 18:35:05
问题 I asked the same on codereview, but they kindly noted that this question is better suited to SO. Consider the following code: #include<vector> #include<memory> template<typename T> struct S final { struct B { virtual void push_back(T&& v) = 0; virtual ~B() { } }; template<class Allocator> struct D final: public B { D(Allocator alloc): vec{alloc} { } void push_back(T&& v) override { vec.push_back(v); } std::vector<T, Allocator> vec; }; S(): S{std::allocator<T>{}} { } template<class Allocator>