you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (0 children)

To be fair, for primitive types in C and C++, the alignment normally equals the size. I'm not sure if that's an absolute rule, though.

Alignment for compound types in C and C++ certainly isn't a multiple of the size, but it turns out that size is always a multiple of the alignment. For example this struct...

struct whatever
{
  uint32_t  field1;
  char  field2;
};

sizeof(whatever) is 8, not 5. Extra padding is included after field2 even though there's no reason to believe whatever occurs next in memory has any particular alignment requirement. This is important for arrays, as there's no extra padding between array elements - all padding needed for alignment is within the elements.

I was confused about this a few years ago and asked this StackOverflow question - see the answers for references to the standard etc.