In this section we will learn about structure padding and why is it needed.
When we are talking about structure or some kind of data structure, there are two important things to take into consideration DATA ALIGNMENT & PADDING. Though these two concepts are different but related.
Before knowing more about Structure padding, one has to understand how machine performs reading and writing operations on memory.Most of the machines are designed in such a way that , memory reading will be efficient if it reads x-sized data stored on a memory location where address%x == zero.
For example consider int size on a machine is 4 bytes, then if that int is stored on location 1002, which is not evenly divisible by 4. Then since that int variable was not aligned according to the machine requirements, so the memory reading performance will not be efficient.Though this machine requirement is not same for all machines. Some machines can read very well though data is not aligned properly. So this requirement of data alignment keeps on changing depending on machines.
Since C’s main aim is to support portable programs, so it should ensure that data should be aligned properly so that it executes on all machines with out much changes.
This is all about alignment of data. Now it is C’s duty to align all the data of a c program has to be aligned properly on memory.By default basic data types of C will be aligned properly on memory. But what about user defined data types like Structs ? Structure will contain different data types . Consider below example.
Struct stud{
char ch; //Assume the starting address as 1000
int i; // what will be the address of this variable then ?
}stud_temp;
In the above example ch is stored at 1000 th location. Variable “i” is supposed to be stored at the location 1001 speaking logically. But by storing in such a way we are committing 2 mistakes. One is we are making this program non portable and other is, it will cause unaligned access of memory since cpu expects int to be stored on some address which is evenly divisible by 4 (i.e 1004 th address in this eg). Such unaligned access will make some systems crash the program.
So the ideal data alignment for above example is 1000 and 1004th byte for ch and i accordingly. So it looks like ..
Struct stud{
char ch; //Assume the starting address as 1000
int i; // what will be the address of this variable then ? answer is ==> 1004
}stud_temp;
Now structure stud_temp instance variables are aligned properly so that it is portable and gives respect to machine architecture and aligns data accordingly .
But did you observe carefully ??? ch is char, it is supposed to take only 1 byte.. but what about 1001,1002,1003 locations ? is it allocated to ch or what happens to it ? Compiler is very intelligent to make those bytes as unused , which means it is just left empty to make alignment proper on memory. this is known as Padding.
This is the basic information about structure padding and alignment properties. I will follow up this post with more information on variety examples to make you understand more about structure padding and alignment and portability.
More later in following posts ..


Latest Comments