You are on page 1of 3

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

}.sizeof(s)). getch().h" void main() { struct st_demo { char ch.ch). } Explanation of the above program: Let assume the size of char is 1 byte and int is 4 bytes(Variable sizes depends on Platforms/Compilers).sizeof(s. According to this. Size of struct is 8 bytes. Note: It is not always necessary that a structure variable contains structure padding or slack bytes. struct st_demo s.For understanding more. 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. int i. #include "stdio. the size of structure variable s should be 5(1+4) bytes. These bytes are called slack bytes. Output of the demo program But from the above programs Output. run the below program and check for memory mapping of the structure variable.i). printf(" Size of int = %d \n\n Size of char = %d \n\n Size of struct = %d ". . sizeof(s.

If anything needs to be added for the above article. Please post it in comment section. .