C Programming and Computer Geeks: Structure padding

Search MY Blog

Showing posts with label Structure padding. Show all posts
Showing posts with label Structure padding. Show all posts

Monday 27 January 2014

Slack Byte and Structure padding in Structures


What is Slack Byte or structure padding in Structure?

CPU/Computer stores structures using the concept of "word boundary".
What is word boundary?
Word boundary is the size of word(bytes) that CPU can process(read/write) at a time. Usually it is the Accumulator size of a processor(CPU). The size of word boundary is machine dependent.

Slack Byte(Padding byte)
In computer with 4 bytes word boundary, the members of structure are stores left aligned on the wordboundary, as shown below. A character data takes one byte and an integer takes 4 bytes. 3 bytes(Padding bytes) between them is left unoccupied. This unoccupied byte is known as the slack byte.

Structure padding
Structure Padding or Slack bytes


Why Padding or slack bytes are required?
Slack byte used for speed optimization. It aligns bytes so that, that can be read/write from structure faster. It is an extra byte, sometimes placed between structure members to align the structure.
Some Platforms like x86, CPU can access data aligned on absolutely any boundary, not only on "word boundary". The mis-aligned access might be less efficient than aligned access. If data is aligned properly, CPU can access the memory(structure members) efficiently and faster.

Note: As far as CPU is concerned, it can access any data on any boundary.

When we declare structure variables, each one of them may contain the slack bytes and the values stored in such slack bytes are undefined(garbage). Due to this, even though the members of two variable values are equal, their structures do not necessarily to be equal. Therefore C does not permit compassion of structures. However, we need to design our own function that can compare the individual members of structure to decide whether the structures are equal or not.

For understanding more, run the below program and check for memory mapping of the structure variable.

#include "stdio.h"
void main()
{
  struct st_demo
   {
char ch;
int i;
   };

  struct st_demo s;

  printf(" Size of int    = %d \n\n Size of char   = %d \n\n Size of struct = %d ",sizeof(s.i), sizeof(s.ch),sizeof(s));
 
  getch();

}

Let assume the size of char is 1 byte and int is 4 bytes(Variable sizes depends on Platforms/Compilers).
According to this, the size of structure variable s should be 5(1+4) bytes.

Slack byte in structures
Output of the demo program
But from the above programs Output, Size of struct is 8 bytes.

So from where we got this extra 3 bytes?
From the memory map we can observe that there are three bytes between variable is unoccupied. These bytes are called slack bytes.

Note: It is not always necessary that a structure variable contains structure padding or slack bytes. 
If anything needs to be added for the above article, Please post it in comment section.


If you liked the above post then don’t forget to share & comment...


Search This Blog