Many of us know that both C and C++ do padding when allocating memory for structure.
But only few know that why the complier does it. In short, for the efficient access of the memory for OS, padding is required.
In long words, the instruction can be fetched/written one machine word at a time. This machine word differs depends on h/w. In a typical 32-bit machines, in one memory attempt, 4 bytes can be either read or written. Thus for the efficient access of memory, our compilers does padding by barring the unused memory.
Let's take an example of a structure with and without having padding.
Without padding :
********************
If we count the size of structure x, it would become 5 bytes only. If the allocation goes as it is (5 bytes), then there is no issue in reading the member x.a. It can be read in single attempt. But for the second member x.b, OS has to do two reads, then calculate the result, return to the user. It holds true and even difficult when the case comes to write. Also, if members are increased, and not aligned properly, OS itself takes time in reading/writing a single member. Consider the following structure.
For the above structure, memory attempt is even much more complicated for any member, thus resulting OS spending more time in accessing these members only.
With Padding:
*****************
Lets take the same structure x again:
Though, we are allocating 3 more bytes than the actual size, each member can be accessed in single attempt. If we consider the second case also, any member can be accessed in single attempt.
One more point to note here is, if we list down the members in ascending order based on the size, we can utilize the memory in efficient manner. For example,
occupies only 8 bytes, where as,
occupies 12 bytes.
Hence, it is always to have the lower size members at first which precede the bigger size members when writing the structures.
Please correct me if I am wrong anywhere and do provide more info about this topic.
||| Dharma |||
But only few know that why the complier does it. In short, for the efficient access of the memory for OS, padding is required.
In long words, the instruction can be fetched/written one machine word at a time. This machine word differs depends on h/w. In a typical 32-bit machines, in one memory attempt, 4 bytes can be either read or written. Thus for the efficient access of memory, our compilers does padding by barring the unused memory.
Let's take an example of a structure with and without having padding.
Code: C
struct x
{
char a;
int b;
}x;
********************
If we count the size of structure x, it would become 5 bytes only. If the allocation goes as it is (5 bytes), then there is no issue in reading the member x.a. It can be read in single attempt. But for the second member x.b, OS has to do two reads, then calculate the result, return to the user. It holds true and even difficult when the case comes to write. Also, if members are increased, and not aligned properly, OS itself takes time in reading/writing a single member. Consider the following structure.
Code: C
struct y
{
char a;
int b;
char c;
char d;
char e;
int f;
} y;
With Padding:
*****************
Lets take the same structure x again:
Though, we are allocating 3 more bytes than the actual size, each member can be accessed in single attempt. If we consider the second case also, any member can be accessed in single attempt.
One more point to note here is, if we list down the members in ascending order based on the size, we can utilize the memory in efficient manner. For example,
Code: C
struct A
{
char x;
char y;
int z;
}A;
Code: C
struct A
{
char x;
int z;
char y;
}A;
Hence, it is always to have the lower size members at first which precede the bigger size members when writing the structures.
Please correct me if I am wrong anywhere and do provide more info about this topic.
||| Dharma |||

