Monday, October 4, 2010

Structure Alignment - my understanding...

The data should be properly aligned for better efficiency. The CPU accesses the memory on WORD (4 byte) boundary...
Thats the reason we add padding bytes in a structure for proper alignment...

The the result of  my research on structure alignment using a gcc compiler is given below... The alignments of the structures are implementation specific... Typically a char will be 1 byte aligned, a short will be 2 byte aligned, an int will be 4 byte aligned and a pointer will be 4 byte aligned... this means that a char can appear at any boundary, a short can appear at any even boundary, and an int can appear at a four byte boundary...

To test the alignment of a structure and the offset of one of its member, i wrote this code:

And the result of alignment was 4.

The result of offset of the integer was 4.

If we use gcc, the size of a long double will be 12 bytes whereas its alignment will be 4 for a IA32 machine.

For different structures, the alignment of its members have influenced the size of them as follows:

typedef struct XYZ
{
//total 12 bytes
/*char c;//1 byte + 3 padding bytes
int x;//4 bytes
short s;//2 bytes
char d;//1 byte + 1 padding byte
}MyStruct1;


typedef struct XYZ
{

//total 8 bytes
/*short s;//2 bytes
char c;//1 byte + 1 padding bytes
int x;//4 bytes*/
}MyStruct2;


typedef struct XYZ
{

//total 12 bytes
/*char a;//1 byte
char c;//1 byte
short s;//2 bytes
int x;//4 bytes
char d;//1 byte + 1 padding byte
short s1;//2 bytes*/
}MyStruct3;


typedef struct XYZ
{

//total 12 bytes
/*char a;//1 byte
char c;//1 byte
short s;//2 bytes
int x;//4 bytes
char d;//1 byte 
char e;//1 byte + 2 padding bytes*/
}MyStruct4;


typedef struct XYZ
{

//total 16 bytes
char a;//1 byte + 1 padding byte
short s1;//2 bytes
char c;//1 byte + 1 padding byte
short s;//2 bytes
int x;//4 bytes
char d;//1 byte + 3 padding byte
}MyStruct5;


typedef struct XYZ
{
//total 20 bytes


char a;//1 byte + 1 padding byte
short s1;//2 bytes
char c;//1 byte + 1 padding byte
short s;//2 bytes
double x;//8 bytes
char d;//1 byte + 3 padding bytes
}MyStruct6;

We can use #pragma pack(1) directive to force the compiler to avoid the padding of the structure elements. Hence with #pragma pack (1) directive, the MyStruct5 will give the size of 11 bytes.







    I would like to end this discussion with an example of how the alignments of the structure members affect the size of a structure.

    Consider the following structure MyStruct1.

    typedef struct XYZ
    {
    //total 12 bytes
    /*char c;//1 byte + 3 padding bytes
    int x;//4 bytes
    short s;//2 bytes
    char d;//1 byte + 1 padding byte
    }MyStruct1;
     its total size is 12 bytes because of padding.

    Now let us reorder the members of the same structure as follows:

    typedef struct XYZ
    {
    //total 8 bytes
    /*char c;//1 byte 
    char d;//1 byte
    short s;//2 bytes
    int x;//4 bytes
    }MyStruct1;

    The size becomes 8 bytes. So there is a reduction of 4 bytes by just reordering the members of the structure. If we happen to do embedded programming or programming for small devices, we need to take care of this fact because memory is a major constraint in these devices...

    No comments:

    Share