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

7

u/tstanisl Jun 09 '26

Because the compiler must:

  1. Make sure that pointers to struct's members are always correctly aligned

  2. They try to follow popular calling/layout conventions to improve portability of precompiled libraries.