3

I have been trying to look up this problem but have not found a solution that works. My compiler is ignoring #pragma pack(push) #pragma pack(2) and __ attribute __ ((aligned (2), packed)) does not solve problem as well. The stack is 8 byte aligned and want to convert some structures to be 2 byte aligned. -fpack-struct works but it affects all structures.

I would like to use #pragma pack.

I am programming a xilinx microblaze in SDK 13.3 eclipse IDE GCC #4.1.2

I guess I dont understand what is making the compiler ignore the Pragma pack.. I dont want to turn the warnings off I would like to use it.

#pragma pack(push)
#pragma pack(2)
struct _Size_Test
{

  union
  {
    struct{
        int8    x;
        int8    y;
     };
     int16    z;
  };
}Size_Test;
#pragma pack(pop)

sizeof(Size_test) = 4 when it should be 2

adding attribute((aligned(2),packed)) does not work

struct _Size_Test
{

  union
  {
    struct{
        int8    x;
        int8    y;
     };
     int16    z;
  };
}Size_Test _attribute_((aligned(2),packed));
microb
  • 93
  • 1
  • 2
  • 9
  • Please show us some code that demonstrates the problem. And be careful with `#pragma pack`; see [this question](http://stackoverflow.com/q/8568432/827263). – Keith Thompson Aug 04 '12 at 00:20

2 Answers2

0

You can use something like this for your struct:

struct __packed__
{
    char* member_one;
    int *member_two;
    .... 
} my_struct;

You can also try to suppress the standard alignment by using this:

int *__unaligned my_int_pointer;

Hope this help.

Regards.

TOC
  • 4,326
  • 18
  • 21
0

The reason the size of your struct is four bytes is due to the inner struct that contains the x and y fields. With the 2-byte packing pragma that you had specified, each of these fields will be 2-byte aligned, making the longest member of the union 4-bytes long.

If compactness is important to you, then use 1-byte packing in conjunction with explicit padding fields. In this particular example, padding isn't even necessary:

#pragma pack(push, 1)

struct _Size_Test
{
    union
    {
        struct
        {
            int8 x;
            int8 y;
        };

        int16 z;
    };
} Size_Test;

The following snippet shows how explicit padding could work:

#pragma pack(push, 1)

struct _Size_Test
{
    union
    {
        struct
        {
            int8 x;
            int8 pad1;
        };

        int16 z;
    };
} Size_Test;