看到網上例如: Ideone example爲什麼在這裏打包結構5而不是4字節的大小?
struct {
union {
struct {
uint32_t messageID : 26;
uint8_t priority : 3;
} __attribute__ ((packed));
uint32_t rawID : 29;
} __attribute__ ((packed));
uint8_t canFlags : 3;
} __attribute__ ((packed)) idSpecial;
爲什麼會編譯器報告結構的大小爲5個字節,而不是4嗎?它應該包含32位。
可能重複[C中的結構和聯合,確定大小和訪問成員](http://stackoverflow.com/questions/3380118/structures-and-unions-in-c -determining-size-and-access-members) – 2015-04-01 07:19:41
@DavidTitarenco:我不認爲這個帖子對位域有好處。如果這個問題已經在其他地方得到了回答,那麼我不會感到驚訝。 – 2015-04-01 07:22:17
響應者應該注意,OP使用[非標準'__attribute__((packed))'](https://gcc.gnu.org/onlinedocs/gcc/Type-Attributes.html)GCC擴展來避免填充。所以,說編譯器被允許插入填充是不正確的 - 當使用'__attribute __((packed))'時,GCC將組織結構以避免填充(在不支持未對齊的讀取的平臺上使訪問更加昂貴)。 – user4815162342 2015-04-01 07:31:27