Recall & Review
beginner
What is struct alignment in C programming?
Struct alignment means arranging data in memory so that each member starts at an address that matches its size or a specific boundary. This helps the processor access data faster.
Click to reveal answer
beginner
What does struct packing do?
Struct packing removes or reduces the padding between members of a struct, making the struct use less memory but possibly slowing down access.
Click to reveal answer
beginner
Why do compilers add padding inside structs?
Compilers add padding to align data members to their natural boundaries, which makes data access faster on the CPU.
Click to reveal answer
intermediate
How can you tell the compiler to pack a struct in embedded C?
You can use compiler-specific directives like
__attribute__((packed)) in GCC or #pragma pack(1) to tell the compiler not to add padding.Click to reveal answer
intermediate
What is a downside of using packed structs?
Packed structs can cause slower access or even hardware faults on some CPUs because data members may not be aligned properly.
Click to reveal answer
What is the main reason for adding padding inside a struct?
✗ Incorrect
Padding aligns data members to their natural boundaries, which helps the CPU access data faster.
Which directive is commonly used in GCC to pack a struct?
✗ Incorrect
GCC uses __attribute__((packed)) to tell the compiler to pack the struct without padding.
What happens if you pack a struct that contains 4-byte integers on a CPU requiring 4-byte alignment?
✗ Incorrect
Packed structs can cause unaligned access, which may slow down or cause errors on some CPUs.
If a struct has members of types char, int, and char, what will the compiler likely do?
✗ Incorrect
The compiler adds padding after the first char so the int starts at a properly aligned address.
What is the effect of #pragma pack(1) in embedded C?
✗ Incorrect
#pragma pack(1) tells the compiler to use 1-byte alignment, effectively removing padding.
Explain what struct packing and alignment mean and why they matter in embedded C programming.
Think about how data is arranged in memory and how CPUs prefer aligned data.
You got /4 concepts.
Describe a scenario in embedded systems where you would want to use struct packing and what risks it might bring.
Consider limited memory devices and hardware communication protocols.
You got /4 concepts.