Data structure alignment is the way data is arranged and accessed in computer memory. Data alignment and Data structure padding are two different issues but are related to each other and together known as Data Structure alignment. Data alignment : Data alignment means putting the data in memory at an address equal to some multiple of the word size. This increases the performance of the system due to the way the CPU handles memory. Data Structure Padding : Now, to align the data, it may be necessary to insert some extra bytes between the end of the last data structure and the start of the next data structure as the data is placed in memory as multiples of fixed word size. This insertion of extra bytes of memory to align the data is called data structure padding. Consider the structure as shown below:
struct
{
char a;
short int b;
int c;
char d;
}
Now we may think that the processor will allocate memory to this structure as shown below:
The total memory allocated in this case is 8 bytes. But this never happens as the processor can access memory with a fixed word size of 4 bytes. So, the integer variable c can not be allocated memory as shown above. An integer variable requires 4 bytes. The correct way of allocation of memory is shown below for this structure using padding bytes.
The processor will require a total of 12 bytes for the above structure to maintain the data alignment. Look at the below C++ program:
CPP
#include <iostream>
using namespace std;
struct test1
{
short s;
int i;
char c;
};
struct test2
{
int i;
char c;
short s;
};
int main()
{
test1 t1;
test2 t2;
cout << "size of struct test1 is " << sizeof(t1) << "\n";
cout << "size of struct test2 is " << sizeof(t2) << "\n";
return 0;
}
Java
public class Main {
static class Test1 {
short s;
int i;
char c;
}
static class Test2 {
int i;
char c;
short s;
}
public static void main(String[] args) {
Test1 t1 = new Test1();
Test2 t2 = new Test2();
System.out.println("size of struct Test1 is " + Integer.BYTES * 2 );
System.out.println("size of struct Test2 is " + Integer.BYTES + 4);
}
}
Output:
size of struct test1 is 12
size of struct test2 is 8
For the first structure test1 the short variable takes 2 bytes. Now the next variable is int which requires 4 bytes. So, 2 bytes of padding are added after the short variable. Now, the char variable requires 1 byte but memory will be accessed in word size of 4 bytes so 3 bytes of padding is added again. So, a total of 12 bytes of memory is required. We can similarly calculate the padding for the second structure also. Padding for both of the structures is shown below:
struct test1
{
short s;
// 2 bytes
// 2 padding bytes
int i;
// 4 bytes
char c;
// 1 byte
// 3 padding bytes
};
struct test2
{
int i;
// 4 bytes
char c;
// 1 byte
// 1 padding byte
short s;
// 2 bytes
};
Note: You can minimize the size of memory allocated for a structure by sorting members by alignment. References : 1) https://en.wikipedia.org/wiki/Data_structure_alignment 2) https://stackoverflow.com/a/119134/6942060
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
30 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Add Your Comment
Please Login to comment...