May uninitialized structure padding bytes lead to undefined behavior?
04:20 28 Oct 2025

Consider the following structure:

struct foo
{
  uint32_t a;
  uint16_t b;
};

Assume that this structure has 2 padding bytes at the end (a likely case) and suppose that an instance is set up like this:

int main(void)
{
  struct foo S;
  uint32_t x = 3;
  uint16_t y = 7;
  memcpy(&S.a, &x, sizeof(x));
  memcpy(&S.b, &y, sizeof(y));
  ...
}

The C standard (6.2.6.1-6) says that "padding bytes take unspecified values when a value is stored in a structure", but in this case I assume that the padding bytes of S are uninitialized.

The question is if the uninitialized padding bytes may lead to undefined behavior when using the structure subsequently? - Or, equivalently, if a compliant compiler is allowed to rely on the values of padding bytes?

c language-lawyer