0
0
Embedded Cprogramming~10 mins

Struct packing and alignment in Embedded C - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Struct packing and alignment
Define struct with members
Compiler checks each member's size
Align members to their natural boundaries
Add padding bytes if needed
Calculate total struct size with alignment
Optionally apply #pragma pack to remove padding
Use packed struct with no padding
Access members carefully to avoid misaligned access
The compiler arranges struct members in memory with padding to align them for efficient access, but packing can remove this padding.
Execution Sample
Embedded C
struct Example {
  char a;
  int b;
  char c;
};
Defines a struct with char, int, and char members to show how padding is added for alignment.
Execution Table
StepMemberOffset Before AlignmentAlignment NeededPadding AddedOffset After Alignment
1a (char)0100
2b (int)1434
3c (char)8108
4Total struct size94312
💡 Struct size aligned to 4 bytes boundary, padding added after 'a' and at end.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
OffsetN/A0 (a)4 (b)8 (c)12 (total size)
Padding bytesN/A0303 (end padding)
Key Moments - 3 Insights
Why is there padding after the first char 'a' before the int 'b'?
Because 'b' is an int and needs to be aligned to a 4-byte boundary, so 3 padding bytes are added after 'a' as shown in execution_table row 2.
Why is the total struct size 12 bytes, not 9?
The struct size must be a multiple of the largest alignment (4 bytes), so 3 padding bytes are added at the end to reach 12 bytes total, as in execution_table row 4.
What happens if we use #pragma pack(1) on this struct?
Padding is removed, so members are packed tightly without gaps, but this can cause slower or unsafe access on some systems.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the offset of member 'b' after alignment?
A1
B4
C3
D8
💡 Hint
Check the 'Offset After Alignment' column for member 'b' in execution_table row 2.
At which step is padding added after member 'a'?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Padding Added' column in execution_table row 2.
If we apply #pragma pack(1), what happens to the total struct size?
AIt becomes 6 bytes
BIt stays 12 bytes
CIt becomes 16 bytes
DIt becomes 4 bytes
💡 Hint
Refer to key_moments answer about packing removing padding and total size.
Concept Snapshot
Struct packing aligns members to their natural boundaries.
Padding bytes are added between members to achieve alignment.
Total struct size is aligned to the largest member's alignment.
#pragma pack(1) removes padding but may cause misaligned access.
Use packing carefully in embedded systems.
Full Transcript
Struct packing and alignment means the compiler arranges struct members in memory so each member starts at an address suitable for its type. For example, an int usually needs to start at an address divisible by 4. If a smaller member like char comes before an int, the compiler adds padding bytes after the char to align the int properly. This padding makes access faster and safer. The total size of the struct is also rounded up to a multiple of the largest alignment to keep arrays of structs aligned. Using #pragma pack(1) tells the compiler to remove padding and pack members tightly, but this can cause slower or unsafe memory access on some systems. The example struct with char, int, char members shows padding added after the first char and at the end to reach 12 bytes total size. Understanding this helps write efficient and correct embedded C code.