0
如何定义一个struct与union里面,这样它只占用1个字节?我试图在任何地方添加__attribute__((packed))
,但它没有帮助。在结构中强制联合对齐
下面是结构的成员:
struct
{
int blockNumber: 1 __attribute__((packed));
int shallBe1: 1 __attribute__((packed)); // should be set to 1 per ISO 14443-4
int nadFollowing: 1 __attribute__((packed)); // always 0 in our software
int cidFollowing: 1 __attribute__((packed)); // always 0 in our software
union
{
int chaining: 2 __attribute__((packed)); // i-block
int ackNak: 2 __attribute__((packed)); // r-block
int deselectOrWtx: 2 __attribute__((packed)); // s-block
} __attribute__((packed));
int blockType: 2 __attribute__((packed));
} pcb __attribute__((packed));
的的sizeof(PCB)返回超过1,而我需要它正好占用一个字节。
以下是我测试结构尺寸:
BYTE testpcb[sizeof(pcb) == 1 ? 0 : -1];
如果我构建过程中出现错误,那么结构是不是1个字节。
尝试使位域“uint8_t”的基础类型改为(或者如果不可用,则使用“unsigned char”)。 – 2015-04-03 08:26:33
没有运气,大小仍然不是1个字节。 – 2015-04-03 08:29:22
每个'int'都是两个字节,即使没有'union',你在struct中也有五个字节。我不明白你怎么能期待整个事情只是一个字节。 – 2015-04-03 08:44:04