Structure Padding in C Language
Categories: C language
Structure padding is a concept in C that adds the one or more empty bytes between the memory addresses to align the data in memory. Let's first understand the structure padding in C through a simple scenario which is given below:
Suppose we create a user-defined structure. When we create an object of this structure, then the contiguous memory will be allocated to the structure members.
struct student
{
char a;
char b;
int c;
} stud1;
In the above example, we have created a structure of type student. We have declared the object of this structure named as 'stud1'. After the creation of an object, a contiguous block of memory is allocated to its structure members. First, the memory will be allocated to the 'a' variable, then 'b' variable, and then 'c' variable.
What is the size of the struct student?
Now, we calculate the size of the struct student. We assume that the size of the int is 4 bytes, and the size of the char is 1 byte.
struct student
{
char a; // 1 byte
char b; // 1 byte
int c; // 4 bytes
}
In the above case, when we calculate the size of the struct student, size comes to be 6 bytes. But this answer is wrong. Now, we will understand why this answer is wrong? We need to understand the concept of structure padding.
Structure Padding
The processor does not read 1 byte at a time. It reads 1 word at a time.
What does the 1 word mean?
If we have a 32-bit processor, then the processor reads 4 bytes at a time, which means that 1 word is equal to 4 bytes.
1 word = 4 bytes
If we have a 64-bit processor, then the processor reads 8 bytes at a time, which means that 1 word is equal to 8 bytes.
1 word = 8 bytes
Therefore, we can say that a 32-bit processor is capable of accessing 4 bytes at a time, whereas a 64-bit processor is capable of accessing 8 bytes at a time. It depends upon the architecture that what would be the size of the word.