What is Slack Byte in Structure?

Computer stores structures using the
concept of “word boundary”. The Size of a word boundary is
machine dependent. In computer with two bytes word boundary, the
members of a structure are stores left aligned on the word boundary,
as shown below. A character data takes one byte and an integer takes
two bytes. One byte between them is left unoccupied. This unoccupied
byte is known as the slack byte.
What is Slack Byte in Structure?
When we declare structure variables,
each one of them may contain slack bytes and the values stored in
such slack bytes are undefined. Due to this, even if the members of
two variables are equal, their structure do not necessarily compare
equal. C, therefore, does not permit compassion of structure.
However, we can design our own function that could compare individual
members to decide whether the structures are equal or not.
Observe the below program and try to
run it.
struct demo
char ch;
float f;
void main()
struct demo d;
What is Slack Byte in Structure?
If we assume size of char and float be
1 byte and 4 bytes respectively. Then according to this the size of
structure variable d should be 5 (1+4) bytes. But as you can
see in above image the output is 8 bytes. This shows that there are
three bytes between them is left unoccupied which is known as slack
One thing we have to keep in mind that
it is not always necessary that a structure variable contains slack
It may be possible that I have missed something in above tutorial, so let me know about it by commenting below. If you liked above article then
don’t forget to comment and share!!

6 thoughts on “What is Slack Byte in Structure?”

  1. You can define a struct with "__attribute__ (__packed__)" to prevent slack bytes, and in that case you can copy all info from an struct to another (from the same type), using memcpy. The exception is when you have pointer attributes into the structure.

  2. prabhanjan j s

    That was good article about slack bytes!!!!!
    But I declared a simple structure like
    struct ex
    float f;
    char ch;
    e1 = {3.5, ‘a’};
    printf(“%d”, sizeof(e1));

    According to us float is 8 bytes and charecter is 1 byte and size of should give minimum 9 bytes (excluding the slack bytes). If slack bytes are included then more than 9 bytes should be printed.
    But I got output as 8 ………….!!!! Why??? How???
    Can please help me and I might have misunderstood the concept of word boundaries and slack byte

    1. here the word boundary is 4 byte so float is 4 byte and char is 4 byte. so output is 8 byte.
      actually float is 4 byte and char is 1 byte, remaining 3 byte is for slack byte.

  3. struct
    int a;
    char x;
    float d;
    void main()
    what is the output of this program,if we take int size 8 bytes.
    and how???.

Leave a Comment

Your email address will not be published. Required fields are marked *