Sunday, 17 March 2013

What Is Structure Padding


Structure padding is done to do memory alignment. As size of Pointer is 4 bytes, If everything is organized by multiple of 4 that it will be easier and faster to calculate the address and processing them. Structures are used to club different size variable. Compiler will align to 4 bytes boundaries and for that it needs padding structure padding is done by the compiler,  and this depends on the some architecture  cannot access the date which will be stored on the odd address, or they may find difficult to access  if this is the reason for padding extra bytes.

Padding will be done by the compiler to structure members and to  be a structure  as a whole also. Compiler padding structure as whole because this allows each member of structure aligned in the array of structure.

  Structure padding example:




 Struct
  {
          char a;   //1 byte
          int b;    //2 byte
          char c;    //1 byte
          long int d;   //4 byte
}

 

Due to structure padding the structure size is 12 bytes

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More