Struct packing and alignment
📖 Scenario: You are working on a small embedded device that communicates sensor data. To save memory and send data efficiently, you need to understand how struct packing and alignment affect the size of your data structures.
🎯 Goal: You will create a struct representing sensor data, then apply packing to reduce its size, and finally print the size of the struct before and after packing.
📋 What You'll Learn
Create a struct called
SensorData with three fields: char id, int value, and char status.Create a variable called
data of type SensorData.Create a packed version of the struct called
PackedSensorData using __attribute__((packed)).Print the size of
SensorData and PackedSensorData using sizeof.💡 Why This Matters
🌍 Real World
Embedded systems often have limited memory and bandwidth. Understanding struct packing helps save memory and send data efficiently over networks.
💼 Career
Many embedded software jobs require knowledge of memory layout and optimization techniques like struct packing to write efficient low-level code.
Progress0 / 4 steps