在 x86-16 位上:
int a; // two bytes
struct{
char b; // One byte.
struct{ // Struct itself is aligned to the size of pointer
short *c[20]; // pointers may be 2 or 4 bytes depending on compile mode.
char d; // one byte
}e;
}f;
double g; // 8 bytes aligned to 8 bytes.
char *h; // 2 or 4 bytes.
在 x86-32 位上:
int a; // four bytes.
struct{
char b; // one byte.
struct{ // struct padding to size of pointer.
short *c[20]; // pointers are 4 bytes.
char d; // one byte.
}e;
}f;
double g; // 8 bytes, aligned to 8 bytes.
char *h; // 4 bytes.
在 x86-64 上:
int a; // 4 bytes.
struct{
char b; // One byte.
struct{ // struct aligned to size of pointer
short *c[20]; // Pointers are 4 or 8 bytes (typically 8)
char d; // One byte.
}e;
}f;
double g; // 8 bytes. Aligned to 8 bytes.
char *h; // 4 or 8 byte pointer, aligned to size of pointer.
在其他一些架构中,这是完全有效的:
int a; // 8 bytes
struct{
char b; // 4 bytes.
struct{ // Struct is not aligned to anything.
short *c[20]; // Pointers are 8 bytes.
char d; // 4 bytes
}e;
}f;
double g; // 12 bytes, aligned to 4 bytes.
char *h; // pointers are 8 bytes.
我会让您为每个示例进行数学运算,以计算实际地址是什么。但就像其他人所说的那样,布局完全取决于编译器,如果不了解特定编译器/处理器架构的规则,就无法确定。