在下面的示例中,我希望 的大小与 : 2 字节complex_t
相同uint16_t
,但它是 3 字节。
删除第二个联合(“proximity_unsafe”)将大小减少到 2 个字节,但我无法弄清楚打包规则的模型。
#include <stdint.h>
#include <stdio.h>
typedef union {
uint16_t unsafe;
struct {
uint16_t backwardmotion_unsafe : 1;
uint16_t batteryvoltage_unsafe : 1;
union {
uint16_t dropoff_unsafe : 4;
struct {
uint16_t dropofffrontleft_unsafe : 1;
uint16_t dropofffrontright_unsafe : 1;
uint16_t dropoffsideleft_unsafe : 1;
uint16_t dropoffsideright_unsafe : 1;
}__attribute__((__packed__));
}__attribute__((__packed__));
union {
uint16_t proximity_unsafe : 3;
struct {
uint16_t proximityfront_unsafe : 1;
uint16_t proximityleft_unsafe : 1;
uint16_t proximityright_unsafe : 1;
}__attribute__((__packed__));
}__attribute__((__packed__));
} __attribute__((__packed__));
} __attribute__((__packed__)) complex_t;
int main()
{
printf("sizeof(complex_t): %i", sizeof(complex_t));
printf("sizeof(uint16_t): %i", sizeof(uint16_t));
}