赛灵思微火焰结构填充/填料

Xilinx Microblaze Structure padding/Packing

本文关键字:填充 填料 结构 火焰      更新时间:2023-10-16

我有两个结构:

Struct _size1 {
    union{
           short a;
           struct {
                    char b;
                    char c;
            }d;
            struct {
                     char x; 
                     char y;
            }z;
     };
     union{
            short a1;
            struct {
                    char b1;
                    char c1;
            }d1;
            struct {
                     char x1; 
                     char y1;
            }z1;
      };
}size1;

和:

Struct _size2 {
    short num; //2 bytes
    short num2; //2 bytes
    short num3; //2 bytes
    size1 st_size; //4 bytes
}size2;
大小

(大小 1) = 4;大小(大小 2) = 12;

我尝试获得的尺寸是 10 的 size2。

它在 num3 和 st_size 之间添加两个字节的填充。Microblaze Compiler 不支持 Pragma Pack

我正在尝试使用属性((打包)),但没有任何成功...

Struct _size2 {
    short num; //2 bytes
    short num2; //2 bytes
    short num3; //2 bytes
    size1 st_size; //4 bytes
}size2 __attribute__((packed));

我不确定出了什么问题,或者这是否以这种方式工作。

谢谢

您正在使用 unsigned int 位字段类型,即 gcc/Microblaze 上的 4 个字节。

使用 gcc

implemantation 定义的unsigned short位字段类型,使用 gcc/Microblaze 的大小为 2 个字节。

我在我的一个头文件中成功地使用它来描述以太网标头:

struct eth_hdr_st {
    unsigned char dst_addr[6];
    unsigned char src_addr[6];
    unsigned short datatype;
} __attribute__ ((__packed__));

然后我像这样使用它:

struct eth_hdr_st eht_header;

注意,第一部分是名为eth_hdr_st的结构的结构定义;第二部分是名为eth_header的变量的声明,类型为struct eth_hdr_st

代码中的错误在于您将这些部分混合在一起,并尝试将__attribute__((packed))应用于名为 size2 的变量。