For example,the default alignment of a union is following:
union{
uint32_t v4;
__uint128_t v6;
}ip;
//in memory
//aaaa
//bbbbbbbbbbbbbbbb
But I want to have a union right aligned:
// aaaa
//bbbbbbbbbbbbbbbb
Is it possible to achieve this in C?
You can use a C11 anonymous struct for this.
union {
#pragma pack(1)
struct {
char padding_[sizeof(__uint128_t) - sizeof(uint32_t)];
uint32_t v4;
};
#pragma pack(0)
__uint128_t v6;
} ip;
// usage
ip.v4 = 0x7F000000;
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