Context
I primirarly code in C. Trying c++.
Not good enough with assembly to tell myself. But improving.
Questions
Do struct AND classes benefit from structure packing in c++ or is it only a c'ish thing?
Do c++ add lot of magic?
Will the private / protected / public segments mess all up?
Thanks
First there are two different terms
data structure alignment
anddata structure padding
.Data alignment means putting the data at a memory offset equal to some multiple of the word size, which increases the system's performance due to the way the CPU handles memory.
To align the data, it may be necessary to insert some meaningless bytes between the end of the last data structure and the start of the next, which is data structure padding.
For your question
"Do struct AND classes benefit from structure packing in c++"
Although C and C++ do not allow the compiler to reorder structure members to save space, other languages might. It is also possible to tell
most C and C++ compilers to "pack" the members
of a structure to a certain level of alignment, e.g. "pack(2)" means align data members larger than a byte to a two-byte boundary so that any padding members are at most one byte long.By changing the ordering of members in a structure, it is possible to change the amount of padding required to maintain alignment. For example, if members are sorted by descending alignment requirements a minimal amount of padding is required which means arranging elements in struct with largest at the beginning.