C++ Home                                                                                                                                                                                                                              www.openskytechnology.comDescription: Description: Description: D:\website\syllabus\study_matreials\c++\logo_pic\logo.jpg

 

 

 

Structure padding

____________________________________________________________________

 

 

Q: What is structure padding and why is it required?

 

Most of the compilers do a specific memory alignment for storing different variables. Normally the alignment depends on the size of the variable. For example char variable (1 byte) can be byte aligned and can appear at any byte boundary.

          short (2 byte) variables must be 2 bytes aligned and they can appear at any even byte boundary. That means 0x10008465 is not a valid location for a short variable but 0x10008466 is valid.

          Similarly long (4 byte) variables  must appear at byte boundaries that are a multiple of 4.

 

          Structure padding occurs because all the structure members must appear at the correct byte boundary. To achieve this compiler adds some padding bytes so that all the structure members appear at the correct byte address. Additionally, the padding bytes can be added at the end of the structure also so that multiple structure variables should be aligned properly in case of array of structures. The following example illustrates this:

 

struct Sample

{

char  c1;

short s1;

char c2;

long L1;

char c3;

};

 

Now, in the above structure c1 can appear at any byte boundary, however s1 must appear at a 2 byte boundary. Hence compiler will add a padding byte between c1 and s1.

          Similarly c2 will appear at the next byte but, L1 (4 byte) will appear only at 4 byte boundary. So the compiler will add 3 padding bytes after c2.

          Again, c3 will appear in the immediate next byte. Now, as the structure contains a long member, the structure must be 4 byte aligned and must be a multiple of 4 bytes in size. Therefore there are 3 padding bytes at the end of the structure.  So, the memory map for the structure would look like:

 

c1

0x10004000       

s1 byte1

0x10004001

s1 byte2

0x10004002

c2

0x10004003

Padding byte

0x10004004       

Padding byte

0x10004005       

Padding byte

0x10004006       

L1 byte1

0x10004007       

L1 byte2

0x10004008       

L1 byte3

0x10004009       

L1 byte4

0x1000400A        

c3

0x1000400B       

Padding byte

0x1000400C       

Padding byte

0x1000400D       

Padding byte

0x1000400E      

 

Size of the structure will be 16 bytes. If you re-write the structure like below:

 

 

 

struct example {

long L1;

short s1;

char c1;

char c2;

char c3;

}

 

This will be of size 12 bytes.

 

L1 member will appear at the correct byte at the beginning. So, no padding required for it. s1 will also appear at the even byte boundary after L1. Hence, no padding needed between L1 and s1. c1, c2 and c3 can appear at any byte boundary. So, no question of padding here also. As the structure contains a long member, it must be a multiple of 4 bytes in size. So, three padding bytes will be added at the end of c3.

 

 

L1 byte1

0x10004000       

L1 byte2

0x10004001

L1 byte3

0x10004002

L1 byte4

0x10004003

s1 byte1

0x10004004       

s1 byte2

0x10004005       

c1

0x10004006       

c2

0x10004007       

c3

0x10004008       

Padding byte

0x10004009       

Padding byte

0x1000400A       

Padding byte

0x1000400B

 

 

Note: You can use pragma to change the byte alignment also. The below example illustrates this:

 

#pragma pack(push,1)

struct Sample

{

char  c1;

short s1;

char c2;

long L1;

char c3;

};

 

#pragma pack(pop)

 

This will change the byte alignment to 1 byte. Now, the size of the above structure will be 9 bytes instead of 16 bytes.

 

 

 

 

 

Corporate Office:  #4, RR Complex, 2nd Floor, Munnekolala, Marathahalli, Bangalore – 560037. (Landmark: near Munnekolala Bus Stop)   Mobile: 9886333765,