Structure Member Alignment, Padding and Data Packing Tutorials
Programming
Structure Member Alignment, Padding and Data Packing in C
When working with structures in C, the size of a structure is often larger than the sum of its members. This happens because of Data Alignment and Structure Padding.
These concepts help the CPU access memory more efficiently and improve program performance.
What is Data Alignment?
Data alignment is the process of storing data at memory addresses that are suitable for the CPU.
Each data type has a natural alignment requirement.
Typical alignment requirements are:
char→ 1 byteshort→ 2 bytesint→ 4 bytesdouble→ 8 bytes
When data is properly aligned, the CPU can read it faster.
What is Structure Padding?
Structure padding is the extra unused memory inserted by the compiler between structure members.
Padding is added to satisfy alignment requirements and improve memory access speed.
Example of Structure Padding
plaintext
plaintext
#include <stdio.h>
struct Student
{
char grade;
int marks;
};
int main()
{
printf("%zu", sizeof(struct Student));
return 0;
}Output
plaintext
8At first glance:
plaintext
char = 1 byte
int = 4 bytes
Total = 5 bytesBut the output is:
plaintext
8 bytesThis is because the compiler adds 3 padding bytes after grade so that marks starts at a 4-byte boundary.
Structure A Example
plaintext
typedef struct
{
char c;
short int s;
} structA;Size Calculation
plaintext
char = 1 byte
padding = 1 byte
short int = 2 bytes
Total = 4 bytesActual Size
plaintext
4 bytesStructure B Example
plaintext
typedef struct
{
short int s;
char c;
int i;
} structB;Size Calculation
plaintext
short int = 2 bytes
char = 1 byte
padding = 1 byte
int = 4 bytes
Total = 8 bytesActual Size
plaintext
8 bytesStructure C Example
plaintext
typedef struct
{
char c;
double d;
int i;
} structC;Size Calculation
plaintext
char = 1 byte
padding = 7 bytes
double = 8 bytes
int = 4 bytes
padding = 4 bytes
Total = 24 bytesActual Size
plaintext
24 bytesThe extra padding at the end ensures proper alignment when creating arrays of structures.
Structure D Example
plaintext
typedef struct
{
double d;
int i;
char c;
} structD;Size Calculation
plaintext
double = 8 bytes
int = 4 bytes
char = 1 byte
padding = 3 bytes
Total = 16 bytesActual Size
plaintext
16 bytesWhy is Structure D Smaller Than Structure C?
Compare:
plaintext
structC
{
char c;
double d;
int i;
};Size = 24 bytes
plaintext
structD
{
double d;
int i;
char c;
};Size = 16 bytes
The difference comes from the order of members.
Placing larger data types first reduces padding and saves memory.
How to Reduce Structure Padding?
A common technique is to arrange structure members from largest size to smallest size.
Better Arrangement
plaintext
struct Student
{
double salary;
int age;
char grade;
};This reduces unnecessary padding.
Structure Packing
Sometimes we do not want the compiler to insert padding bytes.
This process is called Structure Packing.
Structure packing stores members exactly as declared.
Using #pragma pack(1)
plaintext
#pragma pack(1)
struct Student
{
char grade;
int marks;
};Now no padding bytes are added.
Example
plaintext
#include <stdio.h>
#pragma pack(1)
struct Student
{
char grade;
int marks;
};
int main()
{
printf("%zu", sizeof(struct Student));
return 0;
}Output
plaintext
5Without packing:
plaintext
8 bytesWith packing:
plaintext
5 bytesGCC Packed Attribute
GCC also provides another way to pack structures.
plaintext
struct Student
{
char grade;
int marks;
} __attribute__((packed));Advantages of Structure Padding
Faster memory access.
Better CPU performance.
Proper data alignment.
Efficient handling of arrays of structures.
Disadvantages of Structure Padding
Wastes memory.
Structure size becomes larger.
Can cause problems while reading binary files.
When is Structure Packing Used?
Structure packing is commonly used in:
File formats (BMP, JPEG, ELF)
Network packets
Embedded systems
Hardware registers
Communication protocols
Key Points
Data alignment helps the CPU access memory efficiently.
Structure padding adds extra bytes for alignment.
The size of a structure may be larger than the sum of its members.
Member ordering affects structure size.
Placing larger members first reduces padding.
Structure packing removes padding bytes.
#pragma pack(1)and__attribute__((packed))are commonly used for packed structures.
Summary
Data alignment ensures that variables are stored at memory locations preferred by the CPU. To maintain proper alignment, the compiler inserts extra unused bytes called padding inside structures. Padding improves performance but increases memory usage. Structure packing removes these extra bytes and is useful when working with binary files, network protocols, and embedded systems where exact memory layout is required.