x

C – Structure Padding

Prev     Next

  • In order to align the data in memory,  one or more empty bytes (addresses) are inserted (or left empty) between memory addresses which are allocated for other structure members while memory allocation. This concept is called structure padding.
  • Architecture of a computer processor is such a way that it can read 1 word (4 byte in 32 bit processor) from memory at a time.
  • To make use of this advantage of processor, data are always aligned as 4 bytes package which leads to insert empty addresses between other member’s address.
  • Because of this structure padding concept in C, size of the structure is always not same as what we think.

       For example, please consider below structure that has 5 members.


struct student
{
       int id1;
       int id2;
       char a;
       char b;
       float percentage;
};..
  • As per C concepts, int and float datatypes occupy 4 bytes each and char datatype occupies 1 byte for 32 bit processor. So, only 14 bytes (4+4+1+1+4) should be allocated for above structure.
  • But, this is wrong.  Do you know why?
  • Architecture of a computer processor is such a way that it can read 1 word from memory at a time.
  • 1 word is equal to 4 bytes for 32 bit processor and 8 bytes for 64 bit processor. So, 32 bit processor always reads 4 bytes at a time and 64 bit processor always reads 8 bytes at a time.
  • This concept is very useful to increase the processor speed.
  • To make use of this advantage, memory is arranged as a group of 4 bytes in 32 bit processor and 8 bytes in 64 bit processor.
  • Below C program is compiled and executed in 32 bit compiler. Please check memory allocated for structure1 and structure2 in below program.

Example program for structure padding in C language:

Output:

size of structure1 in bytes : 16
Address of id1 = 1297339856
Address of id2 = 1297339860
Address of name = 1297339864
Address of c = 1297339865
Address of percentage = 1297339868
size of structure2 in bytes : 20
Address of id1 = 1297339824
Address of name = 1297339828
Address of id2 = 1297339832
Address of c = 1297339836
Address of percentage = 1297339840

Structure padding analysis for above C program:

Memory allocation for structure1:

Structure padding in C 1_new


  • In above program, memory for structure1 is allocated sequentially for first 4 members.
  • Whereas, memory for 5th member “percentage” is not allocated immediate next to the end of member “c”.
  • There are only 2 bytes remaining in the package of 4 bytes after memory allocated to member “c”.
  • Range of this 4 byte package is from 1297339864 to 1297339867.
  • Addresses 1297339864  and 1297339865 are used for members “name and c”. Addresses 1297339866  and 1297339867 only is available in this package.
  • But, member “percentage” is datatype of float and requires 4 bytes. It can’t be stored in the same memory package as it requires 4 bytes. Only 2 bytes are free in that package.
  • So, next 4 byte of memory package is chosen to store percentage data which is from 1297339868 to 1297339871.
  • Because of this, memory 1297339866 and 1297339867 are not used by the program and those 2 bytes are left empty.
  • So, size of structure1 is 16 bytes which is 2 bytes extra than what we think. Because, 2 bytes are left empty.

 Memory allocation for structure2:

Structure padding in C 2_new

  • Memory for structure2 is also allocated as same as above concept. Please note that structure1 and structure2 are same. But, they differ only in the order of the members declared inside the structure.
  • 4 bytes of memory is allocated for 1st structure member “id1” which occupies whole 4 byte of memory package.
  • Then, 2nd structure member “name” occupies only 1 byte of memory in next 4 byte package and remaining 3 bytes are left empty. Because, 3rd structure member “id2” of datatype integer requires whole 4 byte of memory in the package. But, this is not possible as only 3 bytes available in the package.
  • So, next whole 4 byte package is used for structure member “id2”.
  • Again, 4th structure member “c” occupies only 1 byte of memory in next 4 byte package and remaining 3 bytes are left empty.
  • Because, 5th structure member “percentage” of datatype float requires whole 4 byte of memory in the package.
  • But, this is also not possible as only 3 bytes available in the package. So, next whole 4 byte package is used for structure member “percentage”.
  • So, size of structure2 is 20 bytes which is 6 bytes extra than what we think. Because, 6 bytes are left empty.

How to avoid structure padding in C language?

  • #pragma pack ( 1 ) directive can be used for arranging memory for structure members very next to the end of other structure members.
  • VC++ supports this feature. But, some compilers such as Turbo C/C++ does not support this feature.
  • Please check the below program where there will be no addresses (bytes) left empty because of structure padding.

Example program to avoid structure padding in C:

Output:

size of structure1 in bytes : 14
Address of id1 = 3438103088
Address of id2 = 3438103092
Address of name = 3438103096
Address of c = 3438103097
Address of percentage = 3438103098
size of structure2 in bytes : 14
Address of id1 = 3438103072
Address of name = 3438103076
Address of id2 = 3438103077
Address of c = 3438103081
Address of percentage = 3438103082

Continue on C – typedef….

Prev     Next



Like it? Please Spread the word!