我有以下两个结构:
问题是 sizeof(Content) 返回 160。该结构由 11 个短裤、6 个整数、76 个字符、7 个浮点数、1 个双精度数组成,总共增加了 158 个字节。我数了三遍,还是有2个字节的差别。
typedef struct TIME_T {
short year,mon,day;
short hour,min,sec;
} TIME;
typedef struct {
int no;
char name[20];
char Code[10];
char DASType[10];
short wlen;
float VLtd;
int samp;
int comp;
int locationID;
short TranMode;
char TranIns[12];
short TimerMode;
char ClkType[12];
float ClkErr;
float lat;
float lon;
float alt;
float azimuth,incident;
short weight;
short veloc;
int oritype;
char seismometer[12];
double sens;
TIME start_time;
int record_samples;
} Content;
我写了一小段代码来打印结构中每个变量的位置,突然我发现float wlen
需要 4 个字节。我的代码如下:
int main(void)
{
Content content;
printf("Sizeof Content: %d\n", sizeof(content));
printf("Sizeof int content.no: %d\n", (int)&content.name - (int)&content.no);
printf("Sizeof char[20] content.name: %d\n", (int)&content.Code - (int)&content.name);
printf("Sizeof char[10] content.Code: %d\n", (int)&content.DASType - (int)&content.Code);
printf("Sizeof char[10] content.DASType: %d\n", (int)&content.wlen - (int)&content.DASType);
printf("Sizeof short content.wlen: %d\n", (int)&content.VLtd - (int)&content.wlen);
printf("Sizeof float content.VLtdL %d\n", (int)&content.samp - (int)&content.VLtd);
printf("Sizeof int content.samp: %d\n", (int)&content.comp - (int)&content.samp);
printf("Sizeof int content.comp: %d\n", (int)&content.locationID - (int)&content.comp);
printf("Sizeof int content.locationID: %d\n", (int)&content.TranMode - (int)&content.locationID);
printf("Sizeof short content.TranMode: %d\n", (int)&content.TranIns - (int)&content.TranMode);
printf("Sizeof char[12] content.TranIns: %d\n", (int)&content.TimerMode - (int)&content.TranIns);
printf("Sizeof short content.TimerMode: %d\n", (int)&content.ClkType - (int)&content.TimerMode);
printf("Sizeof char[12] content.ClkType: %d\n", (int)&content.ClkErr - (int)&content.ClkType);
printf("Sizeof float content.ClkErr: %d\n", (int)&content.lat - (int)&content.ClkErr);
printf("Sizeof float content.lat: %d\n", (int)&content.lon - (int)&content.lat);
printf("Sizeof floatcontent.lon: %d\n", (int)&content.alt - (int)&content.lon);
printf("Sizeof floatcontent.alt: %d\n", (int)&content.azimuth - (int)&content.alt);
printf("Sizeof floatcontent.azimuth: %d\n", (int)&content.incident - (int)&content.azimuth);
printf("Sizeof floatcontent.incident: %d\n", (int)&content.weight - (int)&content.incident);
printf("Sizeof short content.weight: %d\n", (int)&content.veloc - (int)&content.weight);
printf("Sizeof short content.veloc: %d\n", (int)&content.oritype - (int)&content.veloc);
printf("Sizeof int content.oritype: %d\n", (int)&content.seismometer - (int)&content.oritype);
printf("Sizeof char[12] content.seismometer: %d\n", (int)&content.sens - (int)&content.seismometer);
printf("Sizeof double content.sens: %d\n", (int)&content.start_time - (int)&content.sens);
printf("Sizeof TIME content.start_time: %d\n", (int)&content.record_samples - (int)&content.start_time);
printf("Sizeof int content.record_samples: %d\n", sizeof(content.record_samples));
getchar();
return 0;
}
输出如下:
Sizeof int content.no: 4
Sizeof char[20] content.name: 20
Sizeof char[10] content.Code: 10
Sizeof char[10] content.DASType: 10
Sizeof short content.wlen: 4
**Sizeof float content.VLtdL 4**
Sizeof int content.samp: 4
Sizeof int content.comp: 4
Sizeof int content.locationID: 4
Sizeof short content.TranMode: 2
Sizeof char[12] content.TranIns: 12
Sizeof short content.TimerMode: 2
Sizeof char[12] content.ClkType: 12
Sizeof float content.ClkErr: 4
Sizeof float content.lat: 4
Sizeof floatcontent.lon: 4
Sizeof floatcontent.alt: 4
Sizeof floatcontent.azimuth: 4
Sizeof floatcontent.incident: 4
Sizeof short content.weight: 2
Sizeof short content.veloc: 2
Sizeof int content.oritype: 4
Sizeof char[12] content.seismometer: 12
Sizeof double content.sens: 8
Sizeof TIME content.start_time: 12
Sizeof int content.record_samples: 4
编译器是 MSVC8,未定义 UNICODE,未定义其他宏。是x86。
我尝试在 gcc 版本 3.4.4 中编译相同的代码,输出是相同的。
Sizeof short content.wlen: 4
谁能给我解释一下?
提前致谢。
编辑:感谢您的回答!我现在得到了。