Question regarding memory allocation of variables in a structure (In C)
本问题已经有最佳答案,请猛点这里访问。
Possible Duplicate:
Why isn't sizeof for a struct equal to the sum of sizeof of each member?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
该代码在IDeone上可用。
为什么结构1(word1)的大小大于结构2(word2)的大小?
这是编译器问题吗?
1 2 | 0 |1 |2 |3 |4 |5 |6 |7 |8 |9 |10 |11 a | (padding) |b |c | (padding) |
1 2 | 0 |1 |2 |3 |4 |5 |6 |7 a |b |(padding)|c |
案例1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 0 1 2 3 4 +---------------------+ | a | Unused | 4 bytes +---------------------+ 0 1 2 3 4 +---------------------+ | b | 4 bytes +---------------------+ 0 1 2 3 4 +---------------------+ | c | Unused | 4 bytes +---------------------+ |
总数:12
案例2:
1 2 3 4 5 6 7 8 9 | 0 1 2 3 4 +---------------------+ | a | b | Unused | 4 bytes +---------------------+ 0 1 2 3 4 +---------------------+ | c | 4 bytes +---------------------+ |
总数:8
P.S:结构填充是实现定义的。