你错过了this - 它说 32 BYTE 对齐,而不是 BIT。
所以你必须做 32 字节对齐而不是 4 字节对齐。
要对齐任何堆栈变量,您可以使用alignas(32) T var;,其中T 可以是任何类型,例如std::array<float, 8>。
要对齐std::vector 的内存或任何其他基于堆的结构alignas(...) 是不够的,您必须编写特殊的对齐分配器(参见Test() 函数的用法示例):
Try it online!
#include <cstdlib>
#include <memory>
// Following includes for tests only
#include <vector>
#include <iostream>
#include <cmath>
template <typename T, std::size_t N>
class AlignmentAllocator {
public:
typedef T value_type;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
typedef T * pointer;
typedef const T * const_pointer;
typedef T & reference;
typedef const T & const_reference;
public:
inline AlignmentAllocator() throw() {}
template <typename T2> inline AlignmentAllocator(const AlignmentAllocator<T2, N> &) throw() {}
inline ~AlignmentAllocator() throw() {}
inline pointer adress(reference r) { return &r; }
inline const_pointer adress(const_reference r) const { return &r; }
inline pointer allocate(size_type n);
inline void deallocate(pointer p, size_type);
inline void construct(pointer p, const value_type & wert);
inline void destroy(pointer p) { p->~value_type(); }
inline size_type max_size() const throw() { return size_type(-1) / sizeof(value_type); }
template <typename T2> struct rebind { typedef AlignmentAllocator<T2, N> other; };
bool operator!=(const AlignmentAllocator<T, N> & other) const { return !(*this == other); }
bool operator==(const AlignmentAllocator<T, N> & other) const { return true; }
};
template <typename T, std::size_t N>
inline typename AlignmentAllocator<T, N>::pointer AlignmentAllocator<T, N>::allocate(size_type n) {
#if _MSC_VER
return (pointer)_aligned_malloc(n * sizeof(value_type), N);
#else
void * p0 = nullptr;
int r = posix_memalign(&p0, N, n * sizeof(value_type));
if (r != 0) return 0;
return (pointer)p0;
#endif
}
template <typename T, std::size_t N>
inline void AlignmentAllocator<T, N>::deallocate(pointer p, size_type) {
#if _MSC_VER
_aligned_free(p);
#else
std::free(p);
#endif
}
template <typename T, std::size_t N>
inline void AlignmentAllocator<T, N>::construct(pointer p, const value_type & wert) {
new (p) value_type(wert);
}
template <typename T, size_t N = 64>
using AlignedVector = std::vector<T, AlignmentAllocator<T, N>>;
template <size_t Align>
void Test() {
AlignedVector<float, Align> v(1);
size_t uptr = size_t(v.data()), alignment = 0;
while (!(uptr & 1)) {
++alignment;
uptr >>= 1;
}
std::cout << "Requested: " << Align << ", Actual: " << (1 << alignment) << std::endl;
}
int main() {
Test<8>();
Test<16>();
Test<32>();
Test<64>();
Test<128>();
Test<256>();
}
输出:
Requested: 8, Actual: 16
Requested: 16, Actual: 16
Requested: 32, Actual: 32
Requested: 64, Actual: 128
Requested: 128, Actual: 8192
Requested: 256, Actual: 256
您可能会在上面的代码中看到,我对 CLang/GCC 使用了 posix_memalign(),对 MSVC 使用了 _aligned_malloc()。从 C++17 开始,还存在 std::aligned_alloc(),但似乎并非所有编译器都实现了它,至少 MSVC 没有。在 CLang/GCC 上,您可以使用 std::aligned_alloc() 而不是 posix_memalign() 作为 @Mgetz 的 commented。
作为英特尔指南says here,您可以使用_mm_malloc() 和_mm_free() 代替posix_memalign()/_aligned_malloc()/_aligned_free()/std::aligned_alloc()/std::free()。