The size of a structure and its alignment directly affect memory and performance; understanding alignment rules, the reasons for padding, and the behavior of bitfields can help write smaller, more efficient, and portable C code.
Why Care About the Memory Layout of Structures
- • In network protocols, binary files, drivers, and embedded systems, the byte layout of structures determines compatibility;
- • Memory alignment can improve access speed but introduces padding bytes;
- • Bitfields can save space but are easily affected by platform and compiler implementations.
Basic Concepts: Alignment and Padding
- • Basic types have alignment requirements (alignment), commonly 1, 2, 4, or 8 bytes;
- • Members are placed according to their alignment requirements, with padding inserted as necessary;
- • The overall size of the structure is aligned to the maximum member alignment value (struct alignment).
Example:
struct A {
char c; // offset 0
int x; // offset 4 (padding 3 bytes after c)
short s; // offset 8
};
// sizeof(struct A) == 12 (assuming int=4, short=2, struct alignment=4)

Reason: The compiler places <span>int</span> at a 4-byte alignment boundary for efficient CPU access, thus inserting 3 bytes of padding after <span>char</span>.
How to View and Verify Layout
- • Real-time view:
<span>sizeof(struct A)</span>; offset view:<span>offsetof(struct A, x)</span>(requires<span>#include <stddef.h></span>); - • Compiler tools:
<span>gcc -fdump-lang-class</span>(complex), commonly used<span>printf("%zu %zu\n", sizeof(s), offsetof(...));</span>example:
#include <stdio.h>
#include <stddef.h>
struct A { char c; int x; short s; };
int main(){
printf("sizeof(A)=%zu\n", sizeof(struct A));
printf("offsetof x=%zu\n", offsetof(struct A, x));
}
Changing Layout: <span>packed</span> and <span>#pragma pack</span>
- •
<span>__attribute__((packed))</span>or<span>#pragma pack(1)</span>can eliminate padding between members, reducing the size of the structure; - • Cost: Unaligned access may lead to performance degradation or even hardware exceptions (on certain architectures); example:
struct B {
char c;
int x;
} __attribute__((packed));
// sizeof(B) == 5
Advice: Use packed only when binary compatibility or memory savings are critically necessary, and use memcpy or byte-wise access when reading/writing unaligned data.
Detailed Explanation and Pitfalls of Bitfields
- • Syntax:
<span>unsigned a:3;</span>indicates occupying 3 bits; the compiler packs bitfields into implementation-defined units (usually<span>int</span>); - • The order of bitfields (from low to high) and cross-byte packing depend on the compiler and machine byte order (endianness);
- • Using bitfields for protocol fields is convenient, but they cannot be used for precise control of bit order across platforms unless compiler behavior is agreed upon simultaneously. example:
struct Flags {
unsigned a:3; // 3 bits
unsigned b:5; // 5 bits -> shares one byte
unsigned c:8; // new storage unit (implementation-dependent)
};

Note:<span>sizeof(struct Flags)</span> is closely related to the internal packing unit implementation and cannot be inferred solely from the number of bits.
Common Misconceptions and Practical Suggestions (Key Points Checklist)
- • Do not use
<span>sizeof</span>as a protocol delimiter; the protocol format should use explicit byte sequences (uint8_t/uint16_t) and manual packing/unpacking; - • If serialization of structures is necessary: define fixed alignment (e.g.,
<span>#pragma pack(1)</span>) and clearly document platform dependencies in the interface documentation; - • For performance-critical paths, adhere to natural alignment to avoid frequent unaligned access by the CPU;
- • Bitfields are suitable for internal state flags, not for field definitions in cross-process or network protocols;
- • Use static assertions (C11
<span>_Static_assert</span>or compile-time<span>sizeof</span>checks) to catch size mismatches at compile time:
_Static_assert(sizeof(struct A) == 12, "struct A size mismatch");
Debugging and Detection Techniques
- • In gdb,
<span>ptype</span>/<span>whatis</span>can be used to view the structure layout; - • Use
<span>padded</span>tools or custom print functions to list the offset of each member; - • Include
<span>sizeof</span>checks in CI to cover different architectures (x86, x86_64, arm) to prevent regressions.
Summary and Publishing Suggestions
- • Understanding alignment and padding is fundamental to writing high-quality C code;
- • Incorporate structure layout into documentation and CI checks in projects to avoid hidden bugs due to ABI/size inconsistencies;
- • Publishing suggestions: include images (structure comparisons, bitfield diagrams), example code, and one small online demo (running sizeof/offsetof).