结构的单字节对齐抵触架构的对齐要求? [英] Struct one-byte alignment conflicted with alignment requirement of the architecture?

查看:174
本文介绍了结构的单字节对齐抵触架构的对齐要求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我previously发布了一个问题,<一个href="http://stackoverflow.com/questions/13881487/should-i-worry-about-the-alignment-during-pointer-casting#comment19155923_13881487">here有关对齐时指针铸造的访问。作为总结,最好不要使用对齐访问是完全可移植的,因为有些建筑可能会引发异常,或者表现可能会非常慢比较一致的访问。

I previously posted a question here about aligned access during pointer casting. As a summary, it's better not to use unaligned access to be fully portable because some architecture may throw an exception, or the performance may get quite slower compared to aligned access.

然而,的情况下,我想用一个字节对齐,例如, transfering网络数据时,我不想增加额外的补白内部结构。所以平时有什么这里的所做的是:

However, there are cases that I want to use one-byte alignment, e.g., during transfering network data, I don't want adding extra paddings inside structure. So usually what's done here is:

#pragma pack (push, 1)
struct tTelegram
{
   u8 cmd;
   u8 index;
   u16 addr1_16;
   u16 addr2_16;
   u8  length_low;
   u8 data[1];
};
#pragma pack (pop)

这时,你可能已经知道了我的问题:如果我执行的单字节对齐对我的结构,这是否意味着它不可能是完全可移植的,因为结构成员不对齐?如果我想既没有填充和便携性?

Then you might already know my question: If I enforce one-byte alignment on my struct, does that mean it cannot be fully portable, because struct members are not aligned? What if I want both no paddings and portability?

谢谢!

推荐答案

首先,未对齐的内存访问是指单件的数据跨越记忆多个单词。例如:在32位系统的32位 INT 地址0,4,8等对齐,但1,2,3,5,6, 7,9等将错位。

Firstly, misaligned memory accesses refers to single pieces of data that span multiple words in memory. For example: on a 32-bit system a 32-bit int at address 0, 4, 8 etc is aligned, but at 1, 2, 3, 5, 6, 7, 9 etc. would be misaligned.

其次,未对齐的数据并不抛出异常在C ++的观点,但可能会引发中断/陷阱/异常在CPU级别 - 例如, SIGBUS在UNIX上,在那里你会通常设置一个信号处理器来应对这一点,但如果你需要解析未对齐的数据在一个可移植的方式,你就不会被抓的信号做 - 你手动$ C C的步骤$打包和解包的数据跨越字边界。

Secondly, misaligned data doesn't "throw an exception" in the C++ sense, but may raise an interrupt/trap/exception at the CPU level - e.g. SIGBUS on UNIX, where you'd generally set a signal handler to react to this, but if you need to parse misaligned data in a portable way you wouldn't do so by catching signals - you'd manually code the steps to pack and unpack data spanning word boundaries.

在你的 tTelegram 结构,数据不错位,但位移位和屏蔽的数据,它的包装/解压缩从寄存器的过程中仍有可能慢 - 需要更多的机code指令 - 比使用数据占用一个独立的字

In your tTelegram struct, the data is not "misaligned", but the process of bit shifting and masking the data as it's packed/unpacked from a register is still likely slower - requiring more machine code instructions - than using data that occupies an independent word.

对于便携性 - 所有非玩具的编译器都会有一个选项,在你所描述的方式来包装,但具体编译会有所不同,在多字节值的字节的布局可能还是大端或little-端(或一些普通怪),而一些CPU允许一些未对齐的数据访问(如86)别人不一样(例如的Ultrasparc)。

Regarding portability - all non-toy compilers will have an option to pack in the way you've described, but the exact pragma will vary, the layout of bytes in multi-byte values may still be big-endian or little-endian (or something plain weird), and while some CPUs allow some misaligned data access (e.g. x86) others don't (e.g. Ultrasparc).

这篇关于结构的单字节对齐抵触架构的对齐要求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆