Trying to use std::aligned_storage with SSE and new

c++, c++11, memory-alignment, memory-management, sse

Solution

It's `aligned_storage<size, align>::type`. `aligned_storage` itself is just a metaprogramming struct.

Also, `new` is only rated to `std::max_align_t`, if I recall correctly, even if you `new` up a type with higher alignment requirements.

Problem

I wanted to try getting a square root of some floats using SSE instrincs in C++. But I get a exception when I try to store the result. Can I use std::aligned_storage like that? ``` #include <iostream> #include <type_traits> #include <xmmintrin.h> using namespace std; using float_storage = aligned_storage<4 * sizeof(float), 16>; int main() { int N; cin >> N; float_storage * values = new float_storage[ N / 4 ]; // 4 floats in pack for(int i = 0; i < N / 4; i++) { void *vptr = static_cast<void*>(&values[i]); float *fptr = static_cast<float*>(vptr); for(int i = 0; i < 4; i++) cin >> fptr[i]; } for(int i = 0; i < N / 4; i++) { void *vptr = static_cast<void*>(&values[i]); float *fptr = static_cast<float*>(vptr); __m128 x = _mm_loadu_ps(fptr); x = _mm_sqrt_ps(x); _mm_store_ps(fptr, x); // im getting an crash here } for(int i = 0; i < N / 4; i++) { void *vptr = static_cast<void*>(&values[i]); float *fptr = static_cast<float*>(vptr); for(int i = 0; i < 4; i++) cout << fptr[i] << endl; } delete[] values; } ```

Original source