allocator

SSE and C++ containers

≡放荡痞女 提交于 2019-11-27 03:29:09
问题 Is there an obvious reason why the following code segfaults ? #include <vector> #include <emmintrin.h> struct point { __m128i v; point() { v = _mm_setr_epi32(0, 0, 0, 0); } }; int main(int argc, char *argv[]) { std::vector<point> a(3); } Thanks Edit: I'm using g++ 4.5.0 on linux/i686, I might not know what I'm doing here, but since even the following segfaults int main(int argc, char *argv[]) { point *p = new point(); } I really think it must be and alignment issue. 回答1: The obvious thing

Questions about Hinnant's stack allocator

旧街凉风 提交于 2019-11-27 03:13:24
I've been using Howard Hinnant's stack allocator and it works like a charm, but some details of the implementation are a little unclear to me. Why are global operators new and delete used? The allocate() and deallocate() member functions use ::operator new and ::operator delete respectively. Similarly, the member function construct() uses the global placement new. Why not allow for any user-defined global or class-specific overloads? Why is alignment set to hard-coded 16 bytes instead of std::alignment_of<T> ? Why do the constructors and max_size have a throw() exception specification? Isn't

Why is allocator::rebind necessary when we have template template parameters?

会有一股神秘感。 提交于 2019-11-26 22:14:30
Every allocator class must have an interface similar to the following: template<class T> class allocator { ... template<class Other> struct rebind { typedef allocator<Other> other; }; }; And classes that use allocators do something redundant like this: template<class T, class Alloc = std::allocator<T> > class vector { ... }; But why is this necessary? In other words, couldn't they have just said: template<class T> class allocator { ... }; template<class T, template<class> class Alloc = std::allocator> class vector { ... }; which is both more elegant, less redundant, and (in some similar

c++ Vector, what happens whenever it expands/reallocate on stack?

我的未来我决定 提交于 2019-11-26 22:04:53
I'm new to C++ and I'm using the vector class on my project. I found it quite useful because I can have an array that automatically reallocates whenever it is necessary (ie, if I want to push_back an item and the vector has reached it's maximum capacity, it reallocates itself asking more memory space to the OS), so access to an element of the vector is very quick (it's not like a list, that to reach the "n-th" element I must go through the "n" first elements). I found this question very useful, because their answers explained perfectly how the "memory allocator" works when I want to store my

How can I create a std::function with a custom allocator?

耗尽温柔 提交于 2019-11-26 21:46:27
问题 To save some code lets say I have a custom allocator named MyAlloc which I have successfully used with a std::vector<int> as follows: std::vector<int,MyAlloc<int>> vec; now I want to save a lambda in a std::function using the custom allocator, how do I do it? My failed attempt: int i[100]; std::function<void(int)> f(MyAlloc<void/*what to put here?*/>{},[i](int in){ //... }); Update: allocators in std::function have been depricated 回答1: According to the standard, you need to give a tag type as

Does std::vector *have* to move objects when growing capacity? Or, can allocators “reallocate”?

冷暖自知 提交于 2019-11-26 20:15:33
A different question inspired the following thought: Does std::vector<T> have to move all the elements when it increases its capacity? As far as I understand, the standard behaviour is for the underlying allocator to request an entire chunk of the new size, then move all the old elements over, then destroy the old elements and then deallocate the old memory. This behaviour appears to be the only possible correct solution given the standard allocator interface. But I was wondering, would it make sense to amend the allocator to offer a reallocate(std::size_t) function which would return a pair

How is a vector's data aligned?

◇◆丶佛笑我妖孽 提交于 2019-11-26 19:49:11
If I want to process data in a std::vector with SSE, I need 16 byte alignment. How can I achieve that? Do I need to write my own allocator? Or does the default allocator already align to 16 byte boundaries? C++ standard requires allocation functions ( malloc() and operator new() ) to allocate memory suitably aligned for any standard type. As these functions don't receive the alignment requirement as an argument, on practice it means that the alignment for all allocations is the same and is the alignment of a standard type with the largest alignment requirement, which often is long double and

Making std::vector allocate aligned memory

佐手、 提交于 2019-11-26 18:45:36
Is it possible to make std::vector of custom structs allocate aligned memory for further processing with SIMD instructions? If it is possible to do with Allocator , does anyone happen to have such an allocator he could share? Edit: I removed the inheritance of std::allocator as suggested by GManNickG and made the alignment parameter a compile time thing. I recently wrote this piece of code. It's not tested as much as I would like it so go on and report errors. :-) enum class Alignment : size_t { Normal = sizeof(void*), SSE = 16, AVX = 32, }; namespace detail { void* allocate_aligned_memory

polymorphic_allocator: when and why should I use it?

本小妞迷上赌 提交于 2019-11-26 17:59:33
问题 Here is the documentation on cppreference , here is the working draft. I must admit that I didn't understand what's the real purpose of polymorphic_allocator and when/why/how I should use it. As an example, the pmr::vector has the following signature: namespace pmr { template <class T> using vector = std::vector<T, polymorphic_allocator<T>>; } What does the polymorphic_allocator offer? What does the std::pmr::vector offer as well in regard of the old-fashioned std::vector ? What can I do now

Compelling examples of custom C++ allocators?

落爺英雄遲暮 提交于 2019-11-26 11:28:36
What are some really good reasons to ditch std::allocator in favor of a custom solution? Have you run across any situations where it was absolutely necessary for correctness, performance, scalability, etc? Any really clever examples? Custom allocators have always been a feature of the Standard Library that I haven't had much need for. I was just wondering if anyone here on SO could provide some compelling examples to justify their existence. timday As I mention here , I've seen Intel TBB's custom STL allocator significantly improve performance of a multithreaded app simply by changing a single