迅雷校招-----第三題
阿新 • • 發佈:2018-11-09
32位機器上,以下結構的sizeof(P)為
struct A {
int a;
char b;
int c;
char d;
}
struct P {
struct A w[2];
short b;
struct A* p;
}
題目連結:https://www.nowcoder.com/test/question/analytic?tid=19096674
/*考察結構體對齊和填充: 結構體每個成員相對於結構體首地址的偏移量都是成員大小的整數倍,如果不是,編譯器會自動在成員間填充。*/ struct A { int a; //4 bytes char b; //1 bytes //char pad[3] //3 bytes int c; //4 bytes char d; //1 bytes //char pad[3] //3 bytes } // total = 16 bytes /* P中有結構體A的成員,但是計算時按照A中資料型別確定的*/ struct P { struct A w[2]; // 2 * 16 bytes short b; //2 bytes //char pad[2] //2 bytes struct A* p; //4 bytes } // total = 40 bytes
可以執行程式檢驗一下: