Why Reordering Structs Doesn't Always Fix Padding
Reordering struct fields can minimize padding, but when arrays enter the picture, the compiler has to enforce stricter alignment rules.
A very common question in C at this stage is what happens if the int is declared first and the char second. At first glance, this seems like it should solve the padding problem completely.
Consider a structure where an int comes first, followed by a char. If the structure starts at address zero, the int occupies bytes 0 through 3, which satisfies its alignment requirement because it starts at an address divisible by four. The char then occupies byte 4, which is also valid because a char only requires one-byte alignment. Inside a single instance of the structure, no padding is required between the fields, and only five bytes are actively used to store data.
However, the story does not end there. In C, a structure’s size is not determined only by how its own fields fit together, but also by how that structure behaves when placed in an array. The C standard guarantees that every element of an array is correctly aligned for all of its members. This means that if an array of such structures is created, each structure must begin at an address that satisfies the strictest alignment requirement of any of its fields. In this case, the int requires four-byte alignment.
If the compiler were to make the structure exactly five bytes long, then the second element of an array would begin at address five. That address is not divisible by four, which would cause the int inside the second structure to be misaligned.
To prevent this, the compiler adds padding at the end of the structure so that the total size becomes a multiple of four. As a result, three padding bytes are appended after the char, making the total size eight bytes. This tail padding ensures that every element in an array starts at a properly aligned address, preserving correctness and portability across all architectures.
This is why reordering fields can reduce *internal* padding but cannot always eliminate padding entirely. The compiler is not wasting memory arbitrarily; it is enforcing alignment guarantees so that your code remains correct, fast, and safe when used in arrays and across different CPU architectures.
When Interview Assignments Turn Into Unpaid R&D
→I built a full-stack product for an interview, got ghosted, and later saw my code running in production. That’s when the pattern became impossible to ignore.
How a Simple Ray Tracer in C Works: From Math to Shadows
→At the heart of ray tracing lies a deceptively simple idea: light travels in straight lines until something blocks it.

