In C/C++ suppose I define a simple struct named test as follows.
struct test
{
double height;
int age;
char gender;
}
For a specific instance of this struct say test A are A.height, A.age, A.gender contiguous
in memory?
More generally, how do the layouts in memory for a Structure of Arrays and an Array of structures look like? A picture would be really helpful.
They will not necessarily be contiguous in memory. This is due to struct padding.
However, in your particular case, it may very well be contiguous. But if you changed the order to something like this:
then they most likely will not be. However, in your particular case, you will still likely get padding after
gender, to realign the struct to 8 bytes.The difference between SoA (Struct of Arrays) and AoS (Array of Structs) would be like this:
SoA:
AoS:
Note that AoS pads within each struct. While SoA pads between the arrays.
These have the following trade-offs: