Structure alignment:
The Structure alignment for 32 bit machine will be in either increasing/decreasing order.
Ex:
Typical Structure alignment
Struct tag
{
char c; //1byte
short int d; //2bytes
int e; //4bytes
double f; //8bytes
};
Structure Padding:
In order to align the data in memory, one or more empty bytes(addresses) are inserted (or left empty) between memory addresses
which are allocated for other structure members while memory allocation. This concept is called structure padding.
Why Structure Padding?
Architecture of a computer processor is such a way that it can read 1 word (4 byte in 32 bit processor) from memory at a time.
To make use of this advantage of processor, data are always aligned as 4 bytes package which leads to insert empty addresses between other member’s address.
Because of this structure padding concept in C, size of the structure is always not same as what we think.
Ex:
struct structure1
{
short int id;
char name[20];
char c;
long percentage;
double marks;
};
We thing that the size of structure becomes (short int)2 bytes+(char name[20])20 bytes+(char c)1 byte+(long percentage)4 bytes+(double marks)8 bytes=35 bytes.
But the actual size of the structure given by compiler is 40 bytes Because of structure padding.
Padding done as follows:
2 Bytes+2 Bytes(padding)+20 bytes+1 byte+3 bytes(padding)+4 bytes+8 bytes=40 Bytes.
If we change the alignment of Structure Like below means:
struct structure2
{
char c;
long percentage;
short int id;
double marks;
char name[20];
};
Then the size of the structure becomes
1byte+3bytes(padding)+4bytes+2bytes+6bytes(padding)+8bytes+20bytes=44 bytes.
So If we align the structure in an order means
struct structure1
{
char c;
short int id;
long percentage;
double marks;
char name[20];
};
Then the size of the structure becomes
1 byte+2bytes+1byte(padding)+4bytes+8bytes+20bytes=36bytes.
So if we aligned a structure in an order(in this example increasing)means we got the size of the structure minimum.
How to avoid Structure padding?
1.By proper aligning of structures we can avoid structure padding.
2.we can also use #pragma pack(1)Function to reduce the structure padding.
#pragma pack(n):
The purpose of #pragma pack is to Sets the alignment of all aggregate members to a specified byte boundary.
Here n is the alignment in bytes, valid alignment values being 1, 2, 4 and 8.
So if we use #pragma pack(1) means we can get the size of the structure as 35 bytes as what we have calculate.
But, If we use #pragma pack() The speed of the processor becomes slow.Because the processor align the members by padding bytes(According to word length of processor) to get high speed.
If we use pragma means at compiler or processor point of view the members are misaligned. So for accessing those members
we require more coding's which leads to PERFORMANCE PENALTY.
Then NEED OF #pragma pack:
We use pragma pack only when our structure match an exact data layout. Ex: Networking protocols and device drivers accessing HW registers.
No comments:
Post a Comment