union w
{ int a; char b; } c;
Union的大小为其内部所有变量的最大值,并且按照类型最大值的整数倍进行内存对齐。
上述w,按int,4字节对齐。因此sizeof(w) = 4.
union w
{ int a; char b[10];double c;
} c;
上述w,按double,8字节对齐。因此sizeof(w) = 10 + 6(对齐) = 16.
本文共 242 字,大约阅读时间需要 1 分钟。
union w
{ int a; char b; } c;
Union的大小为其内部所有变量的最大值,并且按照类型最大值的整数倍进行内存对齐。
上述w,按int,4字节对齐。因此sizeof(w) = 4.
union w
{ int a; char b[10];double c;
} c;
上述w,按double,8字节对齐。因此sizeof(w) = 10 + 6(对齐) = 16.
转载于:https://my.oschina.net/cn0512/blog/514974