Monday, June 13, 2011

Disable structure padding using pragma

Following is the way to disable structure padding using pragma in C.

#pragma pack(push, 1)
//Define your structure here
#pragma pack(pop)
//Structure padding is re enabled.

More information can be found at MSDN. GCC follows the same specifications.

2 comments:

  1. Structure padding is adding extra bits at the end of the structue,so that the structure completes the word boundary.
    http://clinuxpro.com/structure-padding

    ReplyDelete
  2. @Sandhya rephrasing the sentence a little. Structure padding is adding extra "bytes" at the end of structure "members" to make them aligned. For example,
    strcut a {
    char cA;
    char cB;
    int nA;
    }
    2 bytes will be added after char cB to make it 4 bytes region.

    ReplyDelete