Consider working on an x64 bit Windows operation system with the following type alignments:
As far as I understand, it is very bad to do something like this:
struct X_chaotic
{
bool flag1;
double d1;
bool flag2;
double d2;
bool flag3;
double d3;
//... and so on ...
};
According to C++ Alignment, Cache Line and Best Practice and Data structure alignment, it should be better/faster and much more compact to write this:
struct X_alignOrder
{
double d1;
double d2;
double d3;
//... all other doubles ...
bool flag1;
bool flag2;
bool flag3;
//... all other bools ...
};
The members are declared in the order of the alignment size, starting with the highest alignment.
Is it safe to say it is a good idea to order the declaration of the data members by alignment size? Would you say it is best practice? Or does it make no difference?
(I heard that the compiler can not rearrange the defined order, due to the C++ standard, and this even holds for all data members declared in access specifier blocks of a class)
Because I never read about this, neither in Scott Meyers' books nor in Bjarne Stroustrup's books, I wonder if I should start reordering the data declarations by alignment for my day-to-day work.