0

I was going though memory alignment section of C FAQs and found:

struct {long; char; short} would have 1 byte of padding after the char, be the same size as example 7, and have no trailing padding.

Why is there padding after char? Why not after short?

I went though this similar question but its still not clear to me. In this one a user mentioned :

it aligns to the largest alignment of any item in the struct

But here largest item in the struct is long so do we then see next largest alignment while aligning smaller items?

Can someone please help? Thanks!

cheems
  • 164
  • 7
  • 1
    This question settles it down https://stackoverflow.com/questions/4306186/structure-padding-and-packing – Fra93 Oct 03 '22 at 13:27
  • What is unclear? An extra byte is probably placed after the char so that the short will be 2-byte aligned. The whole thing is 8-bytes, which is already 4-byte aligned, so no trailing padding is required to get it to fit in arrays. – Max Oct 03 '22 at 13:35
  • @Max 'Why is there padding after char? Why not after short?' This was unclear, its mentioned in the question. "so that the short will be 2-byte aligned" Whichever answer I went though didn't mentioned this requirement/need. Link by Fra93 made it clear. Thanks – cheems Oct 03 '22 at 13:39

0 Answers0