【发布时间】:2012-12-07 19:09:39
【问题描述】:
我使用 sizeof() 运算符在 C 中打印 sizeof(struct tm),它给了我 44 个字节。但是在 ctime 的手册页中,它有 9 个用于时间的 int 变量。那么它的大小应该是 36。它是如何给出 44 的?
【问题讨论】:
我使用 sizeof() 运算符在 C 中打印 sizeof(struct tm),它给了我 44 个字节。但是在 ctime 的手册页中,它有 9 个用于时间的 int 变量。那么它的大小应该是 36。它是如何给出 44 的?
【问题讨论】:
http://linux.die.net/man/3/ctime
glibc 版本的 struct tm 有额外的字段
long tm_gmtoff; /* Seconds east of UTC */ const char *tm_zone; /* Timezone abbreviation */
这就是你额外字节的来源(可能)。
【讨论】:
glibc 版本的 struct tm 有额外的字段...
long tm_gmtoff; /* Seconds east of UTC */
const char *tm_zone; /* Timezone abbreviation */
再读一遍man ctime..
【讨论】:
除了 RedX 和 Adeel 的非常真实的答案之外,结构内部的填充也可能导致大小大于所有元素大小的总和。要使用自定义结构防止这种情况,您可以使用 GCC __attribute__((__packed__)) 功能。
【讨论】: