我在用eclipse和cygwin。应用程序为64位。在cygwin中,结构被定义为:
struct addrinfo {
int ai_flags; /* input flags */
int ai_family; /* address family of socket */
int ai_socktype; /* socket type */
int ai_protocol; /* ai_protocol */
socklen_t ai_addrlen; /* length of socket address */
char *ai_canonname; /* canonical name of service location */
struct sockaddr *ai_addr; /* socket address of socket */
struct addrinfo *ai_next; /* pointer to next in list */
};结果是48个。socketlen_t的大小为4个字节。int类型大小为4个字节。在64位应用程序中,指针是8个字节。总字节为44(4个ints =16个字节,socket_len =4个字节,3个指针=24个;20+4+24 =44个)。我想知道丢失的4个字节是干什么用的?它们是用来填充的吗?我以为44个字节不需要对齐。有什么想法吗?
谢谢你提前给我答案。
发布于 2016-02-13 22:47:25
结构中的下一个变量是指针,它的长度为64位,并且(在本例中)也将对齐到64位。但是请注意,填充依赖于体系结构和编译器设置;这种情况发生在这里,但并不保证总是会发生。
请注意,由于您正在与操作系统共享此结构,因此不应自行更改填充(大多数编译器都允许使用编译器开关和/或杂注)。操作系统期望这个结构包含一定数量的填充。如果您不能提供它,则结构末尾的所有指针都会被误解其值。
发布于 2016-02-13 22:50:15
https://stackoverflow.com/questions/35386208
复制相似问题