Mr. Boy
Mr. Boy

Reputation: 63816

[Visual C++]Forcing memory alignment of variables/data-structures

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;
};

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?

Upvotes: 0

Views: 1448

Answers (1)

Alex F
Alex F

Reputation: 43331

For static array, you can use

__declspec(align(16)) float data[4];

For dynamically allocated array, use _aligned_malloc and _aligned_free. To control structure elements alignment, use #pragma pack.

Upvotes: 3

Related Questions