简体   繁体   English

c结构的64位对齐/填充?

[英]64 bit alignment/padding for c structure?

struct tag_t_ {
    u_int8_t op;
    u_int8_t num;
    u_int32_t labels[5];
};

In the above structure where will be pad bytes added by 64-bit compiler? 在上面的结构中,将由64位编译器添加填充字节?
Is it before the first label or at the end of first label? 是在第一个标签之前还是在第一个标签的末尾?
If the padding is at end of the first label, Will it cause any erroneous result while accessing (reading) the first label in 32 bit archs? 如果填充位于第一个标签的末尾,那么在访问(读取)32位拱中的第一个标签时是否会导致任何错误结果?

It depends on the compiler, there's no universal rule that can be applied and guaranteed to hold. 这取决于编译器,没有可以应用并保证保持的通用规则。

I'm not sure about the second half of the question, since on the 32-bit architecture the compiler will be different, of course. 我不确定问题的后半部分,因为在32位架构上,编译器当然会有所不同。 It's never a good idea to transfer structures directly. 直接转移结构永远不是一个好主意。

Because of the padding, if you write sizeof (tag_t_) bytes to some external media on the 64-bit machine, transfer the media and and then try to read sizeof (tag_t_) on the 32-bit machine, it will fail. 由于填充,如果您将sizeof (tag_t_)字节写入64位计算机上的某些外部介质,传输介质然后尝试读取32位计算机上的sizeof (tag_t_) ,它将失败。 So don't do that. 所以不要这样做。 Serialize structures field-by-field, and de-serialize them the same way. 逐个字段地序列化结构,并以相同的方式对它们进行反序列化。

I'm running this in 64-bit system -- the memory map of the struct is 我在64位系统中运行它 - 结构的内存映射是

offset:  variable
     0:  op num     
     2:  00 00   // only 2 paddings
     4:  label0
     8:  label1
         ...
    20:  label5

sizeof(struct) == 24 sizeof(struct)== 24

// here one could fit an unsigned short between the chars and the first 32-bit integer without affecting the size of the struct. //这里可以在chars和第一个32位整数之间插入无符号的short,而不会影响struct的大小。

The rule for struct padding is that any basic variable of width W, will be aligned to that width. struct padding的规则是任何宽度为W的基本变量都将与该宽度对齐。 Double as second parameter would cause 7 padding bytes after op and only 3 padding bytes after num , as labels[0] would then start at an offset divisible by 4. 作为第二个参数的double将在op之后导致7个填充字节,并且在num之后仅导致3个填充字节,因为标签[0]将在可被4整除的偏移处开始。

There's a difference between 32/64 bit systems: 32-bit systems will still align 8-byte variables to 32-bit boundaries. 32/64位系统之间存在差异:32位系统仍将8字节变量与32位边界对齐。 64-bit systems will align long int and double to 8-byte boundaries. 64位系统将长整数和双字节对齐到8字节边界。

This would make it safe to use that struct in 32-bit system. 这样可以安全地在32位系统中使用该结构。 If there were doubles in the struct, one could still make the structs compatible with careful planning of the variables. 如果结构中有双精度数,那么仍然可以使结构与仔细规划变量兼容。

padding is usually applied at the end of each field. 填充通常应用于每个字段的末尾。 And no, a 64bit compiled binary is incompatible with a 32bit binary. 不,64位编译二进制文件与32位二进制文​​件不兼容。 So you might have to re-compile everything for 32 bit architecture. 因此,您可能必须重新编译32位架构的所有内容。

The alignment then will be taken care of, by the 32 bit compiler and addresses will be generated accordingly. 然后将通过32位编译器处理对齐,并相应地生成地址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM