好的,我有一个包含a的结构 struct in_addr
元件。
这应该是结构 addrlist_t
至少 12字节大小(8 + 4)。平台是 amd64
。
#include <netinet/in.h> // struct in_addr
#include <stdio.h> // printf()
#include <netdb.h> // toggles size of struct addrlist_t
struct addrlist_t {
struct addrlist_t *next;
struct in_addr h_addr;
};
int main(int argc, char *argv[]) {
printf("%zu + %zu = %zu\n",
sizeof (struct addrlist_t *), sizeof (struct in_addr),
sizeof (struct addrlist_t)
);
return 0;
}
这是完全出乎意料的输出:
$cc main.c -o main -Wall -Wwrite-strings -pedantic -std=gnu99 -Wall -Werror
$./main
8 + 4 = 8
这似乎毫无意义。组合尺寸应至少为12,而不是更小!
但现在,何时 #include <netdb.h>
被删除,预期输出出现:
$./main
8 + 4 = 16
同样的事情发生在 -std=gnu99
被替换为 -std=c99
。
有人可以解释这种行为吗?
为了完整性:
$file main
main: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=709ab89d012d8b5a6ae7423fd80ce643288cba95, not stripped
编辑:格式/单词