cancel
Showing results for 
Search instead for 
Did you mean: 

Default struct fields alignment

CTabo.1
Senior
Hello,
which is the default struct fields alignment policy of GCC, on STM32 MCU?
 
I have the following structure:
 
typedef struct
{
  uint32_t   param1;
  float      param2;
  uint16_t   param3;
} PARAM_T;
 
 
I expected a size of 10 bytes, but it is 12 instead, because a two bytes padding has been inserted between param2 and param3, to align this last one to a 4 bytes multiple address.
 
Why this padding has been added?
'param3' is a uint16_t field, so I expected a 2 bytes address alignment and not a 4 bytes one.
 
Thank you,
Carlo

 

2 REPLIES 2

Why, so the processor doesn't fault.

The CM0(+) being particularly fussy.

Other CMx will fault on misaligned LDRD/STRD.

For compact structures for files perhaps __packed or similar,  but then you must address misalignment issues yourself. 

Situations where you array the data, or make composite structures. 

 

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Thank you @Tesla DeLorean, for the very quick response.

So you are saying that all fields inside a struct, by default, are 4 bytes aligned, despite of their type.
Isn't it?

Thank you,
Carlo