By microb


2012-08-03 20:07:34 8 Comments

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));

2 comments

@Sina Noureddin 2013-03-19 19:36:26

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;

@TOC 2012-08-04 00:27:57

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.

Related Questions

Sponsored Content

10 Answered Questions

[SOLVED] #pragma pack effect

5 Answered Questions

[SOLVED] Is gcc's __attribute__((packed)) / #pragma pack unsafe?

  • 2011-12-19 22:28:00
  • Keith Thompson
  • 112718 View
  • 149 Score
  • 5 Answer
  • Tags:   c gcc pragma-pack

5 Answered Questions

[SOLVED] How exactly does __attribute__((constructor)) work?

  • 2010-01-12 22:43:04
  • Casebash
  • 120226 View
  • 327 Score
  • 5 Answer
  • Tags:   c++ objective-c c gcc

3 Answered Questions

[SOLVED] gcc memory alignment pragma

2 Answered Questions

[SOLVED] Xilinx Microblaze Structure padding/Packing

1 Answered Questions

[SOLVED] Aligning at the end of a packed struct

2 Answered Questions

[SOLVED] Overeager struct packing warnings with `__attribute__((packed))`?

  • 2016-11-16 19:44:23
  • Fake Name
  • 1821 View
  • 4 Score
  • 2 Answer
  • Tags:   c gcc struct arm

1 Answered Questions

[SOLVED] #Pragma pack giving warning when compiling

1 Answered Questions

[SOLVED] Effects of __attribute__((packed)) on nested array of structures?

2 Answered Questions

[SOLVED] Aligned vs. Packed attributes

Sponsored Content