I'm curious about the alignment of uint32_t types on 64-bit platforms. The spec says that uint32_t should be exactly the given bitwidth, which indeed it seems to be:
> printf("sizeof(uint32_t): %zd\n", sizeof(uint32_t));
sizeof(uint32_t): 4
But then I have a struct:
typedef struct A {
uint32_t a;
uint32_t b;
} A;
But, surprisingly:
> printf("sizeof(A): %zd\n", sizeof(A));
sizeof(A): 16
Is uint32_t being 8-byte aligned for some reason? Is it really a 8-byte type underneath?
It is entirely dependent on your compiler and architecture. In your case it looks as if the fields are indeed being 8-byte-aligned, perhaps for performance reasons.
My guess is that by default everything on 64bit architecture will be aligned to 64bit boundaries same as on 32bit architecture everything is aligned to 4 bytes. You can specify packing pragma directives to get rid of the padding. For example
#pragma pack(0)
in gcc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With