关于32位/64位版本头文件的重要

1.今天在测试产品发现一个十分异常的现象,代码从语法上分析完全正确,代码如下

2.在32位系统上编译可以通过,其中第15行出现警告:warning: initialization makes pointer from integer without a cast,

  但执行时,正常结束

3.在64位linux系统编译可以通过,其中第15行出现警告:warning: initialization makes pointer from integer without a cast,

  但在执行时出现 Segmentation fault (core dumped),0x0000003791f3386f in __strlen_sse42 () from /lib64/libc.so.6

  崩溃在 strlen() 处

  分析了半天,且查看man文档,确认自己接口的使用是正确的,最后跟同事交流了一下,提到头文件,跟 inet_ntoa() 的 man 文档

  对比发现,少了第 6 行的头文件,加上后,在64位也就没问题了。

4.今天头一次在64位上测试,遇到这事儿,从找到这个bug的地方、再研究问题原因,花费半天时间,且很是郁闷

5.可以发现,如果在某个方面经检查确认没有错误,就该去其他方面、角度尝试查找失误之处

6.本头文件在32位/64位上的确是有区别的

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 #include <sys/socket.h>
 5 #include <netinet/in.h>
 6 //#include <arpa/inet.h>
 7 
 8 int main(int agrc, char **argv[])
 9 {
10     unsigned long hostip = 0;
11     hostip = inet_addr("130.168.9.104");
12 
13     struct in_addr myaddr;
14     myaddr.s_addr = hostip;
15     char *pip = inet_ntoa(myaddr);
16 
17     printf("... ip is %s, len = %d
", pip, strlen(pip));
18     return 0;
19 }
原文地址:https://www.cnblogs.com/ljtknowns/p/5873360.html