I'm curious about the offsets of C++ struct members when the members are of mixed types. For instance:
#include <iostream>
#include <cstddef>
struct S {
char c0;
char carray[10];
char c1;
double d1;
};
int main()
{
std::cout
<< "offset of char c0 = " << offsetof(S, c0) << '\n'
<< "offset of double carray = " << offsetof(S, carray) << '\n'
<< "offset of double c1 = " << offsetof(S, c1) << '\n'
<< "offset of double d1 = " << offsetof(S, d1) << '\n';
}
The output is the following:
offset of char c0 = 0
offset of double carray = 1
offset of double c1 = 11
offset of double d1 = 16
The offsets for c0
, carray
, and c1
are all easily understandable. I'm curious about d1
. Why does it have an offset of 16 instead of the expected value of 12? It looks like it goes through some offset alignment.