使用sys/socket.h宏时出现神秘的类型转换警告
我正在尝试使用GCC 9.1(iso9899:1999)和GNU make 4.2解决Solaris 11 64位C代码库中的转换警告,我遇到了以下问题:使用sys/socket.h宏时出现神秘的类型转换警告,c,gcc,type-conversion,solaris,gcc-warning,C,Gcc,Type Conversion,Solaris,Gcc Warning,我正在尝试使用GCC 9.1(iso9899:1999)和GNU make 4.2解决Solaris 11 64位C代码库中的转换警告,我遇到了以下问题: warning: unsigned conversion from ‘int’ to ‘long unsigned int’ changes value from ‘-8’ to ‘18446744073709551608’ [-Wsign-conversion] 187 | char ccmsg[CMSG_SPACE(sizeo
warning: unsigned conversion from ‘int’ to ‘long unsigned int’ changes value from ‘-8’ to ‘18446744073709551608’ [-Wsign-conversion]
187 | char ccmsg[CMSG_SPACE(sizeof(int))];
| ^~~~~~~~~~
我知道CMSG_SPACE
在sys/socket.h
中定义为:
/* Amount of space + padding needed for a message of length l */
#define CMSG_SPACE(l) \
((unsigned int)_CMSG_HDR_ALIGN(sizeof (struct cmsghdr) + (l)))
然而,我不明白转换发生在哪里以及如何解决它。谷歌对此毫无帮助
编辑
以下是标题文件中的更多信息,请参见注释:
#if defined(__sparc)
/* To maintain backward compatibility, alignment needs to be 8 on sparc. */
#define _CMSG_HDR_ALIGNMENT 8
#else
/* for __amd64 (and other future architectures) */
#define _CMSG_HDR_ALIGNMENT 4
#endif /* defined(__sparc) */
#define _CMSG_DATA_ALIGNMENT (sizeof (int))
#define _CMSG_HDR_ALIGN(x) (((uintptr_t)(x) + _CMSG_HDR_ALIGNMENT - 1) & \
~(_CMSG_HDR_ALIGNMENT - 1))
#define _CMSG_DATA_ALIGN(x) (((uintptr_t)(x) + _CMSG_DATA_ALIGNMENT - 1) & \
~(_CMSG_DATA_ALIGNMENT - 1))
#define CMSG_DATA(c) \
((unsigned char *)_CMSG_DATA_ALIGN((struct cmsghdr *)(c) + 1))
-8
来自~(\u CMSG\u HDR\u校准-1)
,而长无符号int
转换来自uintptpr\u t
在应用&
运算符之前,编译器警告将-8
转换为uintpttr\t
注:答案来自@jxh留下的评论
以及如何定义
\u CMSG\u HDR\u ALIGN
?您可以使用-E
调用GCC以查看扩展源代码的外观。这应该会给你一些提示。-8
来自~(\u CMSG\u HDR\u ALIGNMENT-1)
,而长无符号int
转换来自uintptpr\t
。编译器在应用&
运算符之前警告将-8
转换为uintpttr\t
。这充其量只是一个虚假警告。将-8
转换为无符号长整型
的过程明确包括:“否则,如果新类型是无符号的,则通过反复增加或减去新类型中可以表示的最大值的一个值来转换该值,直到该值在新类型的范围内。”