Struct padding and alignment in embedded C
You declare a struct with a byte, a 32-bit word and another byte, and sizeof says 12, not 6. Where did six bytes go? Padding. On a RAM-starved microcontroller with an array of a few hundred of these, that is a real cost.
The rule
Each member must start at an address that is a multiple of its alignment requirement. The compiler inserts unused bytes to make that true, and then pads the end of the struct so that every element of an array of the struct is aligned too. The exact alignment rules are implementation-defined; the numbers below are what you get on common 32-bit ARM and 64-bit x86 compilers for 8-, 16- and 32-bit types.
See it with offsetof
#include <stdint.h>
#include <stddef.h>
#include <stdio.h>
struct bad { uint8_t a; uint32_t b; uint8_t c; };
struct good { uint32_t b; uint8_t a; uint8_t c; };
_Static_assert(sizeof(struct bad) == 12, "bad should be 12 on this ABI");
_Static_assert(sizeof(struct good) == 8, "good should be 8 on this ABI");
int main(void)
{
printf("struct bad : size %zu, offsets a=%zu b=%zu c=%zu\n",
sizeof(struct bad), offsetof(struct bad, a), offsetof(struct bad, b), offsetof(struct bad, c));
printf("struct good: size %zu, offsets b=%zu a=%zu c=%zu\n",
sizeof(struct good), offsetof(struct good, b), offsetof(struct good, a), offsetof(struct good, c));
printf("saving per element: %zu bytes; for 300 elements: %zu bytes\n",
sizeof(struct bad) - sizeof(struct good), 300 * (sizeof(struct bad) - sizeof(struct good)));
return 0;
}
struct bad : size 12, offsets a=0 b=4 c=8 struct good: size 8, offsets b=0 a=4 c=5 saving per element: 4 bytes; for 300 elements: 1200 bytes
Compiled with -Wall -Wextra -Werror and run under AddressSanitizer + UBSan.
The same six bytes of data take 12 bytes in one order and 8 in the other. Notice that good still has 2 bytes of tail padding: 4 + 1 + 1 = 6, rounded up to a multiple of 4.
Rules of thumb
- Order members by alignment, largest first.
- Group members of the same size together.
- Use
_Static_assert(sizeof(...) == N)for any struct whose size matters, so a compiler or port change fails the build instead of corrupting data. - Remember the tail padding when you build arrays.
Learning this for work or an interview? Lessons 1 and 2 of DSA for Firmware are free, with labs and C exercises. Start Lesson 1
Why __attribute__((packed)) is not the fix
Packing removes padding by telling the compiler the struct may be unaligned. That is a compiler extension, not ISO C. On some cores (Cortex-M0, for example) unaligned word access faults; on others it works but is slower, and taking a pointer to a packed member can produce an invalid pointer type. Use it only where a layout is dictated by hardware, and then access members through the packed struct itself, not through pointers to its fields.
Do not send a struct over a wire or store it raw
Even with identical source code, another compiler, another optimization setting, or another CPU can give a different layout and different byte order. For a protocol or a flash record, serialize each field explicitly:
#include <stdint.h>
#include <stdio.h>
struct rec { uint8_t kind; uint32_t value; uint16_t crc; };
/* little-endian wire format: 1 + 4 + 2 = 7 bytes, independent of padding */
static void rec_put(uint8_t *p, const struct rec *r)
{
p[0] = r->kind;
p[1] = (uint8_t)(r->value);
p[2] = (uint8_t)(r->value >> 8);
p[3] = (uint8_t)(r->value >> 16);
p[4] = (uint8_t)(r->value >> 24);
p[5] = (uint8_t)(r->crc);
p[6] = (uint8_t)(r->crc >> 8);
}
static void rec_get(const uint8_t *p, struct rec *r)
{
r->kind = p[0];
r->value = (uint32_t)p[1] | ((uint32_t)p[2] << 8) | ((uint32_t)p[3] << 16) | ((uint32_t)p[4] << 24);
r->crc = (uint16_t)(p[5] | (p[6] << 8));
}
int main(void)
{
struct rec in = {7, 0xA1B2C3D4u, 0xBEEFu}, out;
uint8_t wire[7];
rec_put(wire, &in);
rec_get(wire, &out);
printf("sizeof(struct rec)=%zu, wire bytes=%zu\n", sizeof in, sizeof wire);
printf("bytes: %02X %02X %02X %02X %02X %02X %02X\n",
wire[0], wire[1], wire[2], wire[3], wire[4], wire[5], wire[6]);
printf("round trip: %s\n", (in.kind == out.kind && in.value == out.value && in.crc == out.crc) ? "ok" : "FAIL");
return 0;
}
sizeof(struct rec)=12, wire bytes=7 bytes: 07 D4 C3 B2 A1 EF BE round trip: ok
Compiled with -Wall -Wextra -Werror and run under AddressSanitizer + UBSan.
The wire format is now 7 bytes, byte order is explicit, and it does not depend on the compiler. This is also how you should write records to flash.
Visual example: why five bytes become eight
Take a character of size and alignment one and an integer of size and alignment four on an illustrative ABI. The character occupies byte 0, unused padding occupies bytes 1 through 3, and the integer occupies bytes 4 through 7. The resulting structure has size eight and alignment four. Reordering these two fields alone moves padding to the end rather than reducing the total size.
| Byte offsets | Contents |
|---|---|
| 0 | Character |
| 1–3 | Internal padding |
| 4–7 | Integer |
See the interactive memory-and-padding experiment in the Firmware Visual Lab. For a different processor or compiler, inspect sizeof, _Alignof and offsetof instead of assuming these offsets.
Interview check: why is tail padding necessary?
In an array, the next element starts exactly sizeof bytes after the previous one. Rounding the structure size to meet its alignment requirement ensures every element's members can remain correctly aligned. Padding is not part of a stable serialization contract; read struct layout and serialization for a portable representation.
DSA for Firmware teaches it one real bug at a time.
- 28 lessons, each starting from a real firmware bug.
- 110 C exercises with tests, and a reference solution for every one.
- Lessons 1 and 2 are free. No sign-up.
Get the next article by email
One short email when a new article or free lesson is published. No spam. Unsubscribe at any time by replying to any email. See the privacy policy.
Keep reading
- C struct layout is not a binary serialization format
- What
volatilereally does in C, and what it does not - Storing settings in flash to recover from interrupted writes
- Firmware and embedded C interview questions, with answers
- Learn embedded C programming
- A practical firmware interview preparation roadmap
- All articles