我有以下结构:如何将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