How to ensure a member is 4-byte aligned?
In order to use OSAtomicDecrement (mac-specific atomic operation), I need to provide a 4-byte aligned SInt32.
Does this kind of cooking work ? Is there another way to deal with alignment issues ?
struct SomeClass {
SomeClass() {
member_ = &storage_ + ((4 - (&storage_ % 4)) % 4);
*member_ = 0;
}
SInt32 *member_;
struct {
SInt32 a;
开发者_如何学Go SInt32 b;
} storage_;
};
If you're on a Mac, that means GCC. GCC can auto align variables for you:
__attribute__((__aligned__(4))) int32_t member_;
Please note that this is not portable across compilers, as this is GCC specific.
I would guess that any SInt32 is already aligned, even on a mac.
To clarify:
struct Foo {
SInt32 member;
};
member is always aligned properly, unless you pack the structure and put member after a char.
int
s are 4 byte aligned by default with any of the OS X compilers. All you need to do is not intentionally break that alignment (e.g. by doing improper pointer casts, marking your structure as packed
, etc).
I know nothing about Mac programming but on minicomputers that I used to work on, pointers were always aligned on 4-byte (word) boundaries. IIRC, structs were too. Allocated memory always was.
If your compiler supports TR1 (or C++0x), you can use the std::aligned_storage
template.
To allocate space for an object with size S
and alignment A
, you can allocate an object of type std::aligned_storage<S, A>::storage
.
(The namespace may vary between compilers. I think TR1 doesn't specify which namespace the extensions must be placed in. On MSVC, the namespace std::tr1
is used)
Apart from this, 32-bit integers are already 4-byte aligned by the compiler (at least on the platforms where the natural alignment of 32-bit ints is 4 bytes)
If you want to force a nice alignment in a structure, you can use bit fields.
struct
{
Foo _hisFoo;
unsigned int dummyAlignment : 0;
Foo _myFoo;
}
精彩评论