Xilinx Microblaze Structure padding/Packing

1.3k Views Asked by At

I have two structures :

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;

and:

Struct _size2 {
    short num; //2 bytes
    short num2; //2 bytes
    short num3; //2 bytes
    size1 st_size; //4 bytes

}size2;

The sizeof(size1) = 4; The sizeof(size2) = 12;

The size I am trying to obtain is 10 for size2.

It is adding two bytes of padding between num3 and st_size.. Microblaze Compiler does not support Pragma Pack

I am trying to use attribute((packed)) but have not had any success...

Struct _size2 {
    short num; //2 bytes
    short num2; //2 bytes
    short num3; //2 bytes
    size1 st_size; //4 bytes

}size2 __attribute__((packed));

I'm not sure whats wrong or if this even works this way.

Thanks

2

There are 2 best solutions below

0
On

You are using the unsigned int bit-field type which is 4 bytes on gcc / Microblaze.

Use the gcc implemantation-defined unsigned short bit-field type to have a size of 2 bytes with gcc / Microblaze.

0
On

I am successfully using this in one of my header files to describe an Ethernet header:

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

Then I use it just like this:

struct eth_hdr_st eht_header;

NOTE that the first part is the struct definition of a struct named eth_hdr_st; the second part is the declaration of a variable named eth_header, of type struct eth_hdr_st

What's wrong in your code is that you are mixing those parts together, and are trying to apply __attribute__((packed)) to the variable named size2.