Struct packing and alignment in Embedded C - Time & Space Complexity
When working with structs in embedded C, how the data is packed and aligned affects how fast the program runs.
We want to see how the program's speed changes as the struct size or layout changes.
Analyze the time complexity of the following code snippet.
struct Data {
char a;
int b;
char c;
};
void process(struct Data *arr, int n) {
for (int i = 0; i < n; i++) {
arr[i].b += 1;
}
}
This code loops through an array of structs and updates one field in each struct.
- Primary operation: Looping through each struct in the array.
- How many times: Exactly once for each element, so n times.
As the number of structs grows, the loop runs more times, so the work grows directly with n.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 updates |
| 100 | 100 updates |
| 1000 | 1000 updates |
Pattern observation: The work grows in a straight line as n increases.
Time Complexity: O(n)
This means the time to finish grows directly with the number of structs you process.
[X] Wrong: "Changing struct packing will change how many times the loop runs."
[OK] Correct: The loop count depends only on n, not on how the struct fields are arranged or padded.
Understanding how struct layout affects memory but not loop count helps you explain performance clearly and confidently.
"What if we added a nested loop inside process that compares each struct to every other struct? How would the time complexity change?"
Practice
struct packing do in embedded C?Solution
Step 1: Understand struct packing purpose
Struct packing removes padding bytes between fields to reduce memory usage.Step 2: Compare with alignment
Alignment adds padding for faster access, packing removes it to save space.Final Answer:
Removes extra spaces between fields to save memory -> Option AQuick Check:
Struct packing = removes padding [OK]
- Confusing packing with alignment
- Thinking packing changes data types
- Assuming packing initializes values
Solution
Step 1: Recall GCC packing syntax
GCC uses __attribute__((packed)) after struct keyword or at end of struct definition.Step 2: Identify correct placement
struct __attribute__((packed)) MyStruct { int a; char b; }; places __attribute__((packed)) correctly after struct keyword.Final Answer:
struct __attribute__((packed)) MyStruct { int a; char b; }; -> Option DQuick Check:
GCC packing syntax = __attribute__((packed)) [OK]
- Using __packed keyword which is not standard GCC
- Placing __attribute__((aligned)) instead of packed
- Putting attributes in wrong position
struct Example {
char a;
int b;
};
sizeof(struct Example);What is the likely output on a 32-bit system where int is 4 bytes and alignment is on 4-byte boundaries?
Solution
Step 1: Analyze struct member sizes and alignment
char a = 1 byte, int b = 4 bytes aligned on 4-byte boundary.Step 2: Calculate padding and total size
After char a, 3 bytes padding added to align int b. Total size = 1 + 3 + 4 = 8 bytes. Struct size must be multiple of largest alignment (4), which 8 satisfies.Final Answer:
8 -> Option BQuick Check:
Size with padding = 8 bytes [OK]
- Ignoring padding bytes
- Adding padding after last member unnecessarily
- Assuming size is sum of member sizes only
struct Data {
char c;
int i;
} __attribute__((packed));
struct Data d;
printf("%zu", sizeof(d));The output is 5, but the program crashes when accessing
d.i. What is the likely cause?Solution
Step 1: Understand packing effect on alignment
Packing removes padding, so int i may not be aligned on 4-byte boundary.Step 2: Recognize hardware alignment requirements
Some systems crash or fault when accessing unaligned int variables.Final Answer:
Accessing unaligned int causes crash on some systems -> Option AQuick Check:
Unaligned access = crash risk [OK]
- Blaming printf format specifier
- Assuming packing always safe
- Ignoring hardware alignment rules
char a; short b; int c;
On a 32-bit system, how can you arrange and pack this struct to minimize memory size without causing unaligned access faults?
Solution
Step 1: Understand alignment requirements
int requires 4-byte alignment, short 2-byte, char 1-byte.Step 2: Arrange members largest to smallest
Ordering int c; short b; char a; places largest first, minimizing padding.Step 3: Use packing carefully
Packing removes padding but can cause unaligned access; here, ordering avoids unaligned access.Final Answer:
Order members as int c; short b; char a; and use packing -> Option CQuick Check:
Largest to smallest + packing = minimal size safe [OK]
- Ordering members smallest to largest causing padding
- Packing without considering alignment faults
- Ignoring hardware alignment rules
Ready to prove it?
Pass the test and this pattern moves unknown
