[Visual C ++]强制变量/数据结构的内存对齐 [英] [Visual C++]Forcing memory alignment of variables/data-structures

查看:196
本文介绍了[Visual C ++]强制变量/数据结构的内存对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆