It is efficient for data to be lined up on boundaries that match the register size of the propcessor. This is called word boundary. For a 32-bit chip, 32-bits of data can be moved from memory to the chip register in one operation.
The compiler knows this so iot attempts to linbe things up on word boundaries.
Take this struct:
-
struct Example
-
{
-
int a;
-
char b;
-
int c;
-
};
-
It would be efficient if a and c were on 32 bit boundaries. To do that the compile may add three pad bytes after b to force c to a 32-bit boundary.
If you do a sizeof(Example) and a sizeof(a) + sizef(b) + sizeof(c) and get different answers, you have enountered padding.