Take the 2-minute tour ×

Passing aligned types or structures with aligned types by value doesn't work with some implementations. This breaks STL containers, because some of the methods (such as resize) take their arguments by value.

I run some tests with Visual Studio 2008 and not entirely sure when and how pass by value fails. My main concern is function foo. It seems to work fine, but could it be a result of inlining or some other coincidence? What if I change its signature to void foo(const __m128&)?

Your input is greatly appreciated. Thank you.

struct A
    __m128 x;
    int n;

void foo(__m128);
void bar(A);

void f1()
    // won't compile
    // std::vector<A> vec1(3);

    // compiles, but fails at runtime when elements are accessed
    std::vector<__m128> vec2(3);

    // this seems to work. WHY???
    std::vector<__m128, some_16_byte_aligned_allocator<__m128> > vec3(3);

    __m128 x;
    A a;

    // passed by value, is it OK?

    // won't compile

EDIT. STL fails even with aligned allocator, because pass by value problem remains.

Found this link pass __m128 by value

share|improve this question
What are the compile errors/warnings you're getting? I assume something like "formal parameter ... won't be aligned"? –  celion Dec 13 '10 at 0:31
This is what I get. error C2719: 'unnamed-parameter': formal parameter with __declspec(align('16')) won't be aligned –  watson1180 Dec 13 '10 at 0:43
In x64, function arguments support 16-byte alignment, so this problem goes away. I know that doesn't solve your immediate problem, but hey, it's marginally better than nothing. ;) –  jalf Dec 13 '10 at 5:44
It turns out that passing __m128 by value is OK. Follow the web link. –  watson1180 Dec 13 '10 at 7:26
What sort of busted compiler "supports" a type for which it can't actually satisfy the alignment requirements? –  Stephen Canon Dec 13 '10 at 7:46

2 Answers 2

up vote 1 down vote accepted

I think the only safe way to do this in general is to pass by reference. Some platforms (e.g. Xbox 360) support passing vector arguments in registers, but I don't think this is possible on x86.

For the std::vector case, you'll need to make sure that the memory allocated is aligned to 16 bytes; otherwise you'll get crashes when you try to perform most operations on unaligned vectors.

If you're supporting multiple platforms, a safe plan is to use a typedef e.g.

typedef const MyVector& MyVectorParameter;

You can then change the typedef on platforms that support vector pass-by-value.

share|improve this answer
Replacing allocator help with std::vector<__m128>, but std::vector<A> still doesn't compile. –  watson1180 Dec 13 '10 at 0:56
Did you check sizeof(A)? If it isn't a multiple of 16 bytes, then subsequent elements of the array-storage allocated by the vector won't be aligned, even if the beginning of the storage is aligned. –  Karl Knechtel Dec 13 '10 at 1:21
On my platform size of A is 32 bytes. Wrong alignment would cause runtime crash, but vector<A> won't compile regardless of allocator. –  watson1180 Dec 13 '10 at 3:12

the oftenly used resize() function is causing all the alignment and perhaps you can try to specialise vector template for __m128 ?

share|improve this answer

Your Answer


