对齐与打包属性 [英] Aligned vs. Packed attributes

查看:77
本文介绍了对齐与打包属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为16位PIC制作固件,并用C(Microchip C30编译器)编写.我的设备从外部设备收到一长串字节,然后尝试将这些字节复制到结构中.结构定义如下:

I am working on firmware for a 16-bit PIC and writing in C (Microchip C30 compiler). My device receives a long list of bytes from an external device, and then I am trying to copy those bytes into a structure. The structure is defined as follows:

typedef struct __attribute__((__packed__)) {
    char    F1Nickname[17];
    char    F2Nickname[17];
    DWORD   F1CurrentPos;
    DWORD   F2CurrentPos;
    WORD    F1CurrentTemp;
    WORD    F2CurrentTemp;
    DWORD   F1MaxPos;
    DWORD   F2MaxPos;
    BYTE    F1TempCompOn;
    BYTE    F2TempCompOn;
    BYTE    CheckSum;
} deviceStatus;

我遇到一个奇怪的问题,只要结构中的字节总数为奇数,我的程序就会冻结并陷入AddressError服务例程中.我可以通过简单地在结构中添加一个额外的字节来解决此问题,但这似乎是一个临时的解决方案.

I was having a strange problem that whenever the total number of bytes in the structure was an odd number my program would freeze and get caught in AddressError service routine. I can fix the problem by simple adding an extra byte to the struct but that seems like a band-aid fix.

我将packed属性放在结构上,因为我想确保编译器没有在变量之间插入任何填充字节.如果发生这种情况,当从接收到的字符数组中复制过来时,我的结构中的值将不正确.

I put the packed attribute on the structure because I wanted to make sure the compiler didn't insert any filler bytes in between my variables. If that were to happen, the values in my structure would be incorrect when copied over from the received character array.

我知道还有一个称为aligned的属性.对齐是将结构的开头对齐到偶数字节还是将结构中的每个项目对齐到偶数字节?您是否认为此处需要aligned属性?如果我将aligned属性添加到此结构,则还应该将其添加到正在发送数据的设备上的结构中,对吗?到目前为止,它们的定义方式与上面显示的完全相同.

I know there is also an attribute called aligned. Does aligned just align the start of the structure to an even byte or does it align every item in the structure to an even byte? Do you think the aligned attribute is required here? If I add the aligned attribute to this structure, I should also add it to the structure on the device that is sending the data, right? As of now they are both defined the exact same way as shown above.

如果我添加了aligned属性,是否应该删除压缩属性?他们基本上不是相反吗?

If I add the aligned attribute should I remove the packed attribute? Don't they basically do the opposite?

推荐答案

某些微处理器体系结构只能在与字边界对齐的地址上进行数据提取,如果它们未与字对齐,则将引发异常.通常,编译器会提供帮助并生成执行必要的技巧的代码,以确保提取的内容与单词对齐,但是编译器似乎并非如此,这就是为什么看到异常的原因.

Certain microprocessor architecture can only do a data fetch on an address that is aligned to word boundaries and will throw an exception if they are not word aligned. Often the compiler will help out and generate code that performs the necessary acrobatics to ensure the fetches are word aligned but this does not seem to be the case with your compiler and that is why you are seeing the exceptions.

在您的情况下,您正在使用struct序列化数据,因此必须打包.在这种情况下,您必须重新排列struct以确保没有跨字边界的读取,否则您将需要使用解压缩的struct并手动序列化数据.

In your case, you are using a struct to serialize data and therefore it must be packed. In this case you must either rearrange your struct to ensure that there are no reads across word boundaries or you will need to use and unpacked struct and hand serialize the data.

这篇关于对齐与打包属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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