views:

165

answers:

1

The documentation for the InterlockedExchangePointer Function states:

On a 64-bit system, the parameters are 64 bits and the Target parameter must be aligned on 64-bit boundaries; otherwise, the function will behave unpredictably. On a 32-bit system, the parameters are 32 bits and the Target parameter must be aligned on 32-bit boundaries.

I'm compiling for 32bits and the program will run on 32bits and 64bits architectures; does the following structure provide pointers which are usable by InterlockedExchangePointer on any (32 or 64bits) running architecture without code adaptation?

template <class T>
struct Foo {
    enum { count = 3 };
    __declspec(align(64)) T objects[count];
};

(I suppose that if a single pointer is aligned on 64bits it's also aligned on 32bits -- even if there is a padding if running on 32bits)

Thanks.

+2  A: 

On 64-bit system program that written for 32-bit platform will work through compatibility layer (WoW, Windows on Windows), and your pointers must be aligned on 32 bits.

But, if you compile your program in 64 bit mode - your pointers will be automaticaly aligned on 64 bits.

werewindle
I messed up with allocated storage and non-pointer member object alignment; I edited the member type (void* array -> T); please review your answer if necessary.
moala
@moala. In any case compiler by default (if thigs like #pragma pack not used) will align pointers on 32/64 bits boundary.
werewindle