Structure Padding
Most processors require specific memory alignment on variables certain types. Normally the minimum alignment is the size of the basic type in question, fo instance this is common
char variables can be byte aligned and appear at any byte boundary
short (2 byte) variables must be 2 byte aligned, they can appear at any even byte boundary. This means that 0x10004567 is not a valid location for a short variable but 0x10004566 is.
long (4 byte) variables must be 4 byte aligned, they can only appear at byte boundarys that are a multiple of 4 bytes. This means that 0x10004566 is not a valid location for a long variable but 0x10004568 is.
Structure padding occurs because the members of the structure must appear at the correect byte boundary, to achieve this the compiler puts in padding bytes (or bits if bit fields are in use) so that the structure members appear in the correct location.
e.g.
struct ex{
int i; //assuming int to take 2 bytes
char c;
int j;
};
Here, size of struct will be 6 bytes because char will use end at an odd location while int j will require it to start with an even location. So, it ends up with padding between variables c and j.
Most processors require specific memory alignment on variables certain types. Normally the minimum alignment is the size of the basic type in question, fo instance this is common
char variables can be byte aligned and appear at any byte boundary
short (2 byte) variables must be 2 byte aligned, they can appear at any even byte boundary. This means that 0x10004567 is not a valid location for a short variable but 0x10004566 is.
long (4 byte) variables must be 4 byte aligned, they can only appear at byte boundarys that are a multiple of 4 bytes. This means that 0x10004566 is not a valid location for a long variable but 0x10004568 is.
Structure padding occurs because the members of the structure must appear at the correect byte boundary, to achieve this the compiler puts in padding bytes (or bits if bit fields are in use) so that the structure members appear in the correct location.
e.g.
struct ex{
int i; //assuming int to take 2 bytes
char c;
int j;
};
Here, size of struct will be 6 bytes because char will use end at an odd location while int j will require it to start with an even location. So, it ends up with padding between variables c and j.
Structure padding is adding extra bits at the end of the structue,so that the structure completes the word boundary.
ReplyDeletehttp://clinuxpro.com/structure-padding
padding is allocation of extra bits to facilitate the processor for easy access. And memory (extra) is added after the variables and hence the whole structure gets a fixed size (depending on the machine you are using). You can verify it with the example on the link provided by you.
ReplyDelete