Slack Byte and Structure padding in Structures | C Programming and Computer Geeks

Search MY Blog

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...


15 comments:

  1. I tried out the same code (above )but it shows me correct answer
    i.e
    Size of int=2
    Size of char=1
    Size of struct=3
    Explain?

    ReplyDelete
    Replies
    1. Hi..
      Its depends on the compiler which you use...
      In most embedded Compilers it will not add any extra bytes..

      Thanks for the question..!!!

      Delete
  2. how can we remove the slack bytes?

    ReplyDelete
  3. i tried the code and got the same output as shown.....but i really wonder that why size of an integer variable is 4bytes as per my knowledge 2bytes is what that is being thaught.

    ReplyDelete
    Replies
    1. C and CPP are architecture dependent languages unlike Java. So the size and range of data types vary depending on the underlying architecture.

      Delete
    2. Yes.. its dependent on underlying architecture...
      for solving this they have introduced "short" and "long" which guarantees you int size 16 and 32 bit...
      Mostly its Compiler dependent..

      Delete
  4. But why is the size of float taken as 4 byte instead of 8 byte..??????

    ReplyDelete
  5. Hi...i have question
    From c++
    array of int or array of object which is faster in comparison .

    ReplyDelete
  6. Thank you for posting such a great article! I found your website perfect for my needs. It contains wonderful and helpful posts. Keep up the good work!. Thank you for this wonderful Article!

    best laptops for vlogging

    ReplyDelete
  7. It contains wonderful and helpful posts. Keep up the good work!. Thank you for this wonderful Article!
    Best Standing Desk Mat by Wirecutter

    ReplyDelete
  8. Great post full of useful tips! My site is fairly new and I am also having a hard time getting my readers to leave comments. Analytics shows they are coming to the site but I have a feeling “nobody wants to be first”.
    net software development

    ReplyDelete

Search This Blog