Warn about struct padding with -Wpadded
Turn on -Wpadded in GCC 14 to list every struct that gets padding bytes, then reorder members from largest to smallest to shrink the type.
Padding is invisible in the source, and -Wpadded makes the compiler say where it inserts some. GCC 14 warns for each hole inside a struct and for the tail padding added to reach the alignment.
#include <stdio.h>
struct Loose { char a; int b; char c; };
struct Tight { int b; char a; char c; };
int main(void) {
printf("%zu %zu\n", sizeof(struct Loose), sizeof(struct Tight));
}pad.c:3:30: warning: padding struct to align 'b' [-Wpadded]
pad.c:3:42: warning: padding struct size to alignment boundary [-Wpadded]
pad.c:4:42: warning: padding struct size to alignment boundary [-Wpadded]
12 8The exact columns differ, but Loose gets a hole before b and tail padding, and Tight only gets tail padding. Putting members in order of decreasing alignment moves 4 of the 12 bytes away. The flag is too noisy to leave on for a whole project, since every struct that is not perfectly packed is reported, including ones you do not control, so turn it on for one header at a time with -Wpadded on the command line of a single file. It does not warn about padding added by __attribute__((aligned)). See the GCC warning options.