Slack byte in structures in C

What is meant by weak byte in structures in C?

+3
source share
2 answers

Bytes are usually filled in to ensure proper data alignment. For instance:

struct x {
    int a;     // four bytes
    char b;    // one byte
               // three bytes slack
    int c;     // four bytes
} xx;

probably will have weak bytes between band cto get con the correct border.

You can verify this by seeing what gives you sizeif(xx)(12 in the above case, although this is implementation dependent).

, () , . , .

+11
struct student 
{
     char a;//it takes 8 byte
     char b;
     char c;
     char d;
     int e;
};

struct student1;
{
     char a;
     int b;
     char c;//it takes 12 byte(suppose sizeof(int)=4;
}

:

____________________
|char|char|char|char|   // one byte for each char so there is no slack=4byte
|____|____|____|____|
____________________
|int |  * | *  |*   |   // int takes for byte;=4byte total space is 4+4=8;
|____|____|____|____|

student1 :

____________________
|char| s  |s   | s  |   // one byte for char
|____|____|____|____|   // s indicates a slack byte..=4byte
____________________
|int |*   |*   |*   |   //int takes four bytes;=4byte
|____|____|____|____|
 ___________________
|char| s  |s   | s  |   // one byte for char
|____|____|____|____|   // s indicates a slack byte..=4byte
+2

Source: https://habr.com/ru/post/1753493/


All Articles