packing - Why doesn't C++ make the structure tighter? -



packing - Why doesn't C++ make the structure tighter? -

for example, have class,

class naive { public: char a; long long b; char c; int d; };

and according testing program, a d built 1 after another, like

a------- bbbbbbbb c---dddd

- means unused.

why not c++ create tighter, like

ac--dddd bbbbbbbb

class , struct members required standard stored in memory in same order in declared. in example, wouldn't possible d appear before b.

also, architectures prefer multi-byte types aligned on 4- or 8-byte boundaries. compiler can leave empty padding bytes between class members.

you can minimize padding reordering members yourself, in increasing or decreasing size order. or compiler might have #pragma pack alternative or similar, seek minimize padding @ possible expense of performance , code size. read docs compiler.

c++ packing

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -