使用sse指令的类为什么要自定义new和delete操作符
最近在用sse写一个效率好一点的向量类,但是运行的时候总会出现access violation的错误。很久以前,我用sse指令尝试写过一些向量运算的代码。当时印象特别深的就是凡是用到__m128类型成员的类,需要自己定义new和delete。今天再次看到当时的程序:
static void* operator new(size_t size)
{
void *p=_aligned_malloc(size,512);
return p;
};
始终不明白的一点就是,那个512是什么意思。随便改成了64,发现也能运行。
但是这个程序频繁出现access violation。
查了google,得到如下答案:
It is much more likely that you're getting this error because dynamically allocated variables are not 16-byte aligned, also not with __declspec(align(16)). You will need _aligned_malloc (http://msdn2.microsoft.com/8z34s9c6.aspx) for that. Another alternative is to use static variables (which the compiler can align on the stack).
于是把64改成16,就运行成功了。
No comments:
Post a Comment