You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 12 Next »

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

In the example below, assuming that sizeof(buf) is equal sizeof(size_t) + (sizeof(char) * 50), which would equal 54 (assuming sizeof(size_t) is 4) is incorrect. The sizeof(buf) may actually evaluate to a larger value due to structure padding.

struct buffer {
    size_t size;
    char buffer[50];
};
  • No labels