Alignment using the STL vector

I want the storage of my vectors to be aligned (say, to the border of 16 bytes), so I have this allocator as a wrapper on memalign() , I wonder if it is right to choose the allocated memory used with STL vectors.

 template <class T , int Alignment=16> class AlignedAllocator { public: ... pointer allocate (size_type size, const_pointer *hint = 0) { return (pointer) memalign(Alignment, size*sizeof (T)); }; void deallocate (pointer p, size_type size) { free(p); }; ... } 

If not, are there implementations available for aligned dispensers that work with STL containers?

PS: I am compiling with gcc.

+4
source share
3 answers

The memalign (3) function is deprecated, instead of posix_memalign (3) instead. Other than that, if ... in your code means to contain the rest of the necessary dispenser elements, your code looks good.

+1
source

Looks nice. But you will have problems porting such code to BSD and Darwin. It is much more reliable to record the distribution function manually.

Just a new big enough block and return the pointer. You will also need to write a custom memory deallocation function.

0
source

It depends on the implementation of the vector, it can choose to allocate a slightly larger buffer and add your data to something that breaks the alignment.

0
source

All Articles