- Muchas notas - Fran Acién

20240717 - C padding and structures

I have the next structure:

typedef struct STBP_SECH_S{
  uint8_t overflow;
  uint32_t time;
} STBP_SECH_S;  // Secondary Header Structure


Why?

sizeof(STBP_SECH_S) = 8

and sizeof(uint8_t) =1, and sizeof(uint32_t) = 4????

Does it make sense?

It is beacuse the compiler puts padding in order to fit the 32 bits time variable. To fix this you need to write:

typedef struct __attribute__((packed)) STBP_SECH_S{
  uint8_t overflow;
  uint32_t time;
} STBP_SECH_S;  // Secondary Header Structure