We know that the correct order of declaring variables in structures changes the size of a structure also because of padding. I have seen the reference here.
Suppose a structure is:
struct s {
char b; //1 for char
char c; //1 for char + 2 for padding
int a; //4 for int
}my_struct;
So the size of the my_struct
is 8
, but without padding it could be 6
which is less than 8
.
So my question is: Why padding is done in structures? What is the necessity of this concept?
Because without padding the structure is of lesser size. My question is not related to when padding take place, it is more concerned about the concept of padding.