r/C_Programming • • Jun 09 '26

packed attribute for structs

Why don't C compilers automatically optimize/pack structures instead of requiring explicit attributes?

2 Upvotes

24 comments sorted by

View all comments

Show parent comment

2

u/duane11583 Jun 09 '26

un aligned on x86_64 is sub optimal this is why the default is the padding and alignment to the cache

it is all about speed.

the x86 has extra hw to handle the unaligned data

4

u/innosu_ Jun 09 '26

Not totally accurate. Modern x86/amd64 only has unaligned penalty when crossing cache line size boundary, typically 64 byte. All unaligned access that does not cross the 64 byte alignment boundary does not have performance penalty at all.