Loading content...
Loading content...
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.
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 byte
short → 2 bytes
int → 4 bytes
double → 8 bytes
When data is properly aligned, the CPU can read it faster.
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.
plaintext
plaintext
#include <stdio.h>
struct Student
{
char grade;
int marks;
};
int main()
{
printf("%zu", sizeof(struct Student));
return 0;
}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.
plaintext
typedef struct
{
char c;
short int s;
} structA;plaintext
char = 1 byte
padding = 1 byte
short int = 2 bytes
Total = 4 bytesplaintext
4 bytesplaintext
typedef struct
{
short int s;
char c;
int i;
} structB;plaintext
short int = 2 bytes
char = 1 byte
padding = 1 byte
int = 4 bytes
Total = 8 bytesplaintext
8 bytesplaintext
typedef struct
{
char c;
double d;
int i;
} structC;plaintext
char = 1 byte
padding = 7 bytes
double = 8 bytes
int = 4 bytes
padding = 4 bytes
Total = 24 bytesplaintext
24 bytesThe extra padding at the end ensures proper alignment when creating arrays of structures.
plaintext
typedef struct
{
double d;
int i;
char c;
} structD;plaintext
double = 8 bytes
int = 4 bytes
char = 1 byte
padding = 3 bytes
Total = 16 bytesplaintext
16 bytesCompare:
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.
A common technique is to arrange structure members from largest size to smallest size.
plaintext
struct Student
{
double salary;
int age;
char grade;
};This reduces unnecessary padding.
Sometimes we do not want the compiler to insert padding bytes.
This process is called Structure Packing.
Structure packing stores members exactly as declared.
plaintext
#pragma pack(1)
struct Student
{
char grade;
int marks;
};Now no padding bytes are added.
plaintext
#include <stdio.h>
#pragma pack(1)
struct Student
{
char grade;
int marks;
};
int main()
{
printf("%zu", sizeof(struct Student));
return 0;
}plaintext
5Without packing:
plaintext
8 bytesWith packing:
plaintext
5 bytesGCC also provides another way to pack structures.
plaintext
struct Student
{
char grade;
int marks;
} __attribute__((packed));Faster memory access.
Better CPU performance.
Proper data alignment.
Efficient handling of arrays of structures.
Wastes memory.
Structure size becomes larger.
Can cause problems while reading binary files.
Structure packing is commonly used in:
File formats (BMP, JPEG, ELF)
Network packets
Embedded systems
Hardware registers
Communication protocols
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.
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.