Tao said:
hi.. Group,
I want to use __m128i with vector, but it seems does not work. The following
code generated a runtime error indicating that there is a memory corruption.
std::vector<__m128i> list;
__m128i x = _mm_set_epi32(0, 0, 0, 2);
list.push_back(x);
Can somebody tell me why this happen?
__m128i requires 16-byte alignment, which vector doesn't guarantee to
provide (well, it should do, but __m128i has especially strict alignment
requirements - ones that aren't met by malloc). One way of working
around this would be to specify a special vector allocator which did
align the memory correctly. e.g. (VC2005 specific due to __alignof())
#include <vector>
#include <memory>
#include <malloc.h>
#include <emmintrin.h>
template <class T>
class aligned_allocator: public std::allocator<T>
{
public:
template <class U>
struct rebind
{
typedef aligned_allocator<U> other;
};
aligned_allocator(){}
template <class U>
aligned_allocator(aligned_allocator<U> const& other){}
template <class U>
aligned_allocator& operator=(aligned_allocator<U> const& other){
return *this;
}
T* allocate(std::size_t N, const void* hint)
{
return allocate(N);
}
T* allocate(size_type N)
{
return static_cast<T*>(_aligned_malloc(N * sizeof(T), __alignof(T)));
}
void deallocate(T* p, std::size_t N)
{
_aligned_free(p);
}
};
Then:
std::vector<__m128i, aligned_allocator<__m128i> > list;
Tom