Skip to main content
Graduate II
June 25, 2024
Question

Default struct fields alignment

  • June 25, 2024
  • 1 reply
  • 1561 views
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

 

    This topic has been closed for replies.

    1 reply

    Graduate II
    June 25, 2024

    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. 

     

    CTabo.1Author
    Graduate II
    June 25, 2024

    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