Search code examples
c++memory-managementmemory-alignmentallocator

Making std::vector allocate aligned memory


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?


Solution

  • Starting in C++17, just use std::vector<__m256i> or with any other aligned type. There's aligned version of operator new, it is used by std::allocator for aligned types (as well as by plain new-expression, so new __m256i[N] is also safe starting in C++17).

    There's a comment by @MarcGlisse saying this, making this an answer to make it more visible.