I am using ARM. I got alignment fault due to read/write in odd offset(we knew ARM is 4 byte aligned). All the structs defined in my program is single – byte aligned like
#pragma pack(push, 1)
typedef struct
{
char a1;
int a2;
char a3;
}a;
#pragma pack(pop)
I am trying to do
#pragma pack(push, 1)
typedef struct
{
char a1 __attribute__ ((aligned (4)));
int a2;
char a3;
}a;
#pragma pack(pop)
the gcc attribute _attribute_ ((aligned (4))) makes no effect.
Note :: The above code is not my actual code. sample scenario.
so I re-arranged structure member to solve the alignment issue. I want to ensure whether the re-arranging is the possible solution or we can make _attribute_ to work on this scenario. Any other solutions are welcome. Thanks in advance.
You can safely read/write char/int in byte-aligned structs on ARM, compiler take care about alignment. Single problem can occur with alignment issue is with the casting to 32 bit int like this:
Note: if you for some reasons like align some member (from begin of the struct) you can use following trick: