According to ISO/IEC 9899-1999,
There may be unnamed padding at the end of a structure or union.
This is often referred to as structure padding. Structure members are arranged in memory as they are declared in the program text. Padding is added to the structure to ensure the structure is properly aligned in memory.
Non-Compliant Code Example
One would expect the the example below to output sizeof(size_t) + (sizeof(char) * 50)
. However, this may not be the case. Assuming sizeof(size_t)
is 4, the example may actually evaluate the size of buf
to be 56 due to padding.
struct buffer { size_t size; char buffer[50]; }; int main(int argc, char *argv[]) { struct buffer buf; printf("%u",sizeof(buf)); return 0; }