[Visual C ++]强制变量/数据结构的内存对齐 [英] [Visual C++]Forcing memory alignment of variables/data-structures
问题描述
我正在使用SSE,我推荐在16byte边界上对齐数据。有两种情况需要考虑:
I'm looking at using SSE and I gather aligning data on 16byte boundaries is recommended. There are two cases to consider:
float data[4];
struct myystruct
{
float x,y,z,w;
};
我不知道第一种情况可以明确地完成,虽然可能有一个编译器选项我可以使用?在第二种情况下,我记得几年前能够控制旧版GCC的打包,这是否仍然可行?
I'm not sure the first case can be done explicitly, though there's perhaps a compiler option I could use? In the second case I remember being able to control packing in old versions of GCC several years back, is this still possible?
推荐答案
对于静态数组,可以使用
For static array, you can use
__declspec(align(16)) float data[4];
对于动态分配的数组,请使用_aligned_malloc和_aligned_free。要控制结构元素对齐,请使用#pragma pack。
For dynamically allocated array, use _aligned_malloc and _aligned_free. To control structure elements alignment, use #pragma pack.
这篇关于[Visual C ++]强制变量/数据结构的内存对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!