Word boundries | slack bytes |structure padding in c -by priyanshu verma

Word boundries and structure padding 
In c programming

As we know ,Structures allows several related objects, called structure members, to be grouped into one aggregate object.



How memory is allocated to structure members?

When an object of some structure type is declared then some contgious block of memory will be allocated to the structure member.

For example:


Struct abc{

Char a; // 1byte

Char b;//1byte

Int c;//4 byte

}var;

Size should be 6 (1+1+4) byte but it might be larger.

Because,

Processor does not read one byte at a time from memory it reads one word at a time.

If you have a 32 bit processor then it means it can access 4 byte at a time which means word size is 4 bytes.

If you have a 64 bit processor then it means it can access 8 byte at a time which means word size is 8 bytes.

The size of a structure member is identical to the size of an ordinary object with the same type as that member. However, the size of a structure might not be equal to the sum of the sizes of its structure members, it might be larger.

Computer stores structures using a concept of "word boundaries" the size of word boundaries is machine dependent in a computer with 2 byte word boundaries the member of a structure is stored left aligned.

The C Standard requires that structure members have addresses that increase in the order in which they are declared. It also states that the address of a structure’s initial member must be the same as the address of the structure itself. However, compilers are allowed to shift the addresses of subsequent members so that they align with address boundaries that make accessing the member efficient for the target device. These shifts mean that there could be gaps in the structure that are not used for storage but that will contribute to the size of the overall structure.


Structure padding:

Struct abc{

char a; //1byte

char b; // 1 byte

char c; // 4 byte

}var;

Whenever we want the values stored in variable C , two cycles are required to access the content of variable c. In first cycle first two byte can be accessed and in second cycle last two bytes.

It is an unnecessary wastage of CPU cycle.

So the correct way to allocate memory to structures variable  is....


In above example the total memory allocated to the structure variable should be 6 (1 byte+ 1 byte+ 4 byte) byte but it'll be allocated 8(1 byte+1 byte+ 2 byte (structure padding) + 4 byte) bytes memory to the structure variable because of structure padding.

Why we could not compared to a structure value was directly using (==) comparision operator??

We could not compare two structures because even if the member of two variables are equal their structures do not necessarily compare equal. C,therefore does not permit comparison of structures. However we can design our own function that could compare individual member to decide whether the structure are equal or not.


Following is the C program for the comparison of structure variables −

struct class{

   int number;

   char name[20];

   float marks;

};

main(){

   int x;

   struct class student1 = {111,"Rao",72.50};

   struct class student2 = {222,"reddy",                                                            67.00};

   struct class student3;

   student3 = student2;

   x = ((student3.number ==                                                   student2.number) &&(student3.marks == student2.marks)) ? 1 : 0;

   if(x == 1){

      printf("\nstudent2 and student3 are                          same\n\n");

      printf("%d %s %f\n", student3.number,

      student3.name,

      student3.marks);

   }

   else

   printf("\nstudent2 and student3 are different\n\n");

}

Output

When the above program is executed, it produces the following output −


student2 and student3 are same

222 Reddy 67.00