#include <iostream>using namespacestd;structa1{intA; Static intb; };structa2{intA; Charc;};structa3{floatA; Charc;};structa4{floatA; intb; Charc;};structa5{DoubleD; floatA; intb; Charc;};voidMain () {cout<<sizeof(A1) <<Endl; cout<<sizeof(A2) <<Endl; cout<<sizeof(A3) <<Endl; cout<<sizeof(A4) <<Endl; cout<<sizeof(A5) <<Endl;}
The analysis is as follows:
A1:sizeof only calculates the size allocated in the stack, and B is a static variable, which is located in the global data area, so it does not occupy the space of the stack, so the size of A1 is only the space occupied by a, that is, 4;
A2: Data alignment, int size is 4;char size is 1, extension is 4, so A2 is 8;
A3: Data alignment: Float size is 4;char size 1, extension is 4, so A3 is 8;
A4: Data alignment: Float size is 4;int size is 4;char size is 1, extension is 4, so A3 is 12;
A5: Data alignment: Double size for 8;float size of 4,int size for 4,float and int for 8;char size of 1, consider alignment, expand to 8; so A4 is 24;
Reprinted from: http://blog.csdn.net/tangbo1987/article/details/6766918
sizeof Data alignment Issues