By default, C types are represented in the machine’s native format and byte order, and properly aligned by skipping pad bytes if necessary (according to the rules used by the C compiler).
Native size and alignment are determined using the C compiler’s sizeof expression. This is always combined with native byte order.
Note the difference between '@' and '=': both use native byte order, but the size and alignment of the latter is standardized.
您可能需要再次阅读struct documentation中的第一个注释。 默认值是C对齐的数据边界,因此一些填充字节负责造成这种差异。因此,添加适当的字节顺序应该可以解决这个问题。在
请参见documentation中有关对齐的部分:
为了说明这一点:
您刚刚遇到了padding,因为首先是字节数据,然后是整数(具有更强的对齐约束)
从documentation:
因此,需要指定一个endianness来禁用填充:
为了完整性进行了编辑,在阅读了Galen的优秀答案之后:如果您不想强制endianness,只指定
=
会更好。在相关问题 更多 >
编程相关推荐