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;
SInt32 b;
} storage_;
};
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 alignmentA
, you can allocate an object of typestd::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)
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 aspacked
, etc).I would guess that any SInt32 is already aligned, even on a mac.
To clarify:
member is always aligned properly, unless you pack the structure and put member after a char.
If you're on a Mac, that means GCC. GCC can auto align variables for you:
Please note that this is not portable across compilers, as this is GCC specific.
If you want to force a nice alignment in a structure, you can use bit fields.