Wednesday, May 21, 2014

Structure Padding in C

The size of a class can be changed simply by playing with the order of its member declarations. Most processors require specific memory alignment on variables of certain types.


Alignment rules:

  • BOOL/BYTE 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 0x0007 is not a valid location for a short variable, but 0x0008 is.
  • INT(4 byte) variables must be 4 byte aligned, they can only appear at byte boundaries that are a multiple of 4 bytes. This means that 0x1006 is not a valid location for a long variable, but 0x1008 is.

So, structure padding occurs because the members of a structure must appear at the correct byte boundary. To achieve this, the compiler puts in padding so that the structure members appear in the correct location. And the size of the structure should be aligned to the memory so that the padding will occur at the end of the structure too.
 Eg.



struct NODE{

            unsigned char a;

            unsigned short b;

            unsigned char c;

            unsigned int d;

};



100
101
102
103
104
105
106
107
108
109
110
111
CHAR
PAD
SHORT
CHAR
PAD
INT



In this example, the alignment is as follows:

  • a (char) can appear at any byte boundary (as per rule 1). So, imagine that the memory allocation for the structure NODE starts at location 0x100 and the first member a takes up that location for its 1 byte size storage.
  • b (short) should have its first byte appearing at an even location. So it can not take up the location 0x101. It will take the locations 0x102 and 0x103. The location 0x101 is padded (as per rule 2).
  • c (char) can then appear in the next available memory location. So it occupies 0x104 (as per rule 1).
  • d (int) must start at a location which is the multiple of 4. The next possible location for d is 0x108. So, there are 3 padding bytes between charand int (as per rule 3).

Thus, the structure would be 12 Bytes long.


For optimization, the structure can be re-written like this:


struct NODE{

            unsigned int d;

            unsigned short b;

            unsigned char a;

            unsigned char c;

};



100
101
102
103
104
105
106
107
INT
SHORT
CHAR
CHAR



Thus, the structure would be 8 Bytes long.



The int appears at the correct byte alignment. The short will be correctly aligned, so no need for padding between the int and short. The two chars can appear at any location. Now, it is only 8 bytes long. So we saved 4 bytes by proper alignment of variables.







How to avoid structure padding using pragma?

The default compiler alignment is of 4 bytes. We need to change it to 1 byte.

For that, do the following steps:

·        Push the current compiler alignment into the stack.

·        Set the alignment into 1 byte.

·        Declare the structure.

·        Restore the default compiler alignment from the stack.



Eg.


#pragma pack(push)         /* Push the current alignment into Stack */

#pragma pack(1)               /* Set alignment to 1 Byte Boundary */


struct NODE{

            unsigned char a;

            unsigned short b;

            unsigned char c;

            unsigned int d;

};


#pragma pack(pop)          /* Pop original alignment from Stack */



Thus, the structure size is 8 Bytes.