我有以下結構:如何將C結構與4字節邊界對齊?
typedef struct LOG_EVENT
{
time_t time;
uint32_t count;
int32_t error_type;
uint16_t gm_state;
uint8_t gm_mode;
uint8_t mc_state;
} LOG_EVENT;
在32位的系統,所述結構的strict alignment
是4字節,因此部件被分佈在4字節邊界對齊。在這種情況下沒有添加填充,因爲所有成員一起都是4字節對齊的。
但在64位系統上這不是真的,其中time_t
是64位。這種情況下的strict alignment
是8字節。
如何將對齊方式更改爲4字節?我想在64位系統上跨4個字節的邊界對齊,因爲我想確保沒有填充完成。
從海灣合作委員會的屬性頁https://gcc.gnu.org/onlinedocs/gcc-3.2/gcc/Variable-Attributes.html,它說,The aligned attribute can only increase the alignment; but you can decrease it by specifying packed as well
。
我沒有看到packed
屬性接受任何參數。
另外,如果我使用如下的字節對齊,會造成任何問題,相對於4字節對齊:
typedef struct __attribute__ ((packed)) LOG_EVENT
{
time_t time;
uint32_t count;
int32_t error_type;
uint16_t gm_state;
uint8_t gm_mode;
uint8_t mc_state;
} LOG_EVENT;
@yantantphil我不確定這是個好主意。 – Monku
「在32位系統上,結構嚴格對齊是4字節」 - 錯誤....不!任何平臺上的對齊都不能保證。但隨時提供一個參考標準說明不同。這是一個像您的其他問題一樣的XY問題。 **爲什麼**你想要這種對齊?看起來你應該重新考慮你的整個設計。 – Olaf
@Olaf我想要4字節對齊,因爲我不希望編譯器添加填充。如果你看看這個結構,考慮嚴格的對齊是8字節,最後增加4字節。 – Monku