Jump to content
  • Advertisement
Sign in to follow this  
David Neubelt

Aligning structure members

This topic is 3416 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey, I need to align structure members to 16 byte boundaries. I tried struct Foo { __declspec(align(16)) volatile unsigned int* data0; __declspec(align(16)) volatile unsigned int* data1; __declspec(align(16)) volatile unsigned int* data2; __declspec(align(16)) volatile unsigned int* data3; }; Though, the members were aligned to 8 byte boundaries. I was thinking of doing this __declspec(align(16)) struct Foo { volatile unsigned int* data0; uint32 padding0; uint32 padding1; volatile unsigned int* data1; uint32 padding2; uint32 padding3; }; Though, that assumes pointers are 8 bytes so I'd have to make defines to determine how much padding are between each variable per platform. Any other ideas? I'm only having this problem with visual studio not other compilers :( -= Dave

Share this post


Link to post
Share on other sites
Advertisement
Strange. Under MVC2008:
struct Foo {
__declspec( align( 16 ) ) volatile unsigned int* data0;
__declspec( align( 16 ) ) volatile unsigned int* data1;
__declspec( align( 16 ) ) volatile unsigned int* data2;
__declspec( align( 16 ) ) volatile unsigned int* data3;
};

int main()
{
Foo foo;
printf("%d %d\n", sizeof(Foo), sizeof(foo));
printf("%x %d\n", &foo.data0, offsetof(Foo, data0));
printf("%x %d\n", &foo.data1, offsetof(Foo, data1));
printf("%x %d\n", &foo.data2, offsetof(Foo, data2));
printf("%x %d\n", &foo.data3, offsetof(Foo, data3));
return 0;
}
results in:
64 64
12ff30 0
12ff40 16
12ff50 32
12ff60 48


That failing to work, there's always the possibility of using a union. Perhaps:
struct DataPtr {
union {
volatile int * data;
char padding[16];
} value;
volatile int * operator->() { return value.data; }
volatile int * operator*() { return value.data; }
};

struct Foo2 {
DataPtr data0;
DataPtr data1;
DataPtr data2;
DataPtr data3;
};
Granted, it's a bit messy.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!