关于阿里的一道笔试题分析
其题目如下:
#pragma pack(2) class A { public: int i; union U { char buff[13]; int i; }u; void foo() { } typedef char* (*f)(void*); enum{red, green, blue} color; }a;
class A 在32位 sizeof的结果为?
答案是22.
首先,我们看到在代码头部的申明:
#pragma pack(2)
表示使用2个字节对齐,也就是说,每个成员的内存地址,相对与基地址的偏移量是能够与2整除的。
使用以下代码进行分析:
#include <iostream> using namespace std; int main(int argc, const char * argv[]) { cout << "siize of class A is:" << sizeof(a) << endl; cout << "offset of A.i is:" <<(size_t)&(((A*)0)->i) << endl; cout << "offset of A.u is:" <<(size_t)&(((A*)0)->u) << endl; cout << "offset of A.color is:" <<(size_t)&(((A*)0)->color) << endl; cout << "seize of A.u is:" << sizeof(a.u) << endl; cout << "seize of A.u.buff is:" << sizeof(a.u.buff) << endl; return 0; }
输出结果:
siize of class A is:22 offset of A.i is:0 offset of A.u is:4 offset of A.color is:18 seize of A.u is:14 seize of A.u.buff is:13
可以看到,i成员的地址偏移量为0,占用4个字节,4%2 = 0,所以 成员u的便宜为 4。
成员u是一个共同体,共同体的大小为其占用空间最大的成员的大小,这里是 A.u.buffer。
A.b.buffer 为char数组 占用空间为: 1 * 13,由于 13%2 != 0,编译器会补上一个字节使其对其: 1*13+1 % 2 = 0。
于是下一个成员的偏移为:4+14 = 18,成员 color的偏移为18,color为 enum,占用4个字节,所以sizeof(A) = 18 + 4 = 22