我正在尝试使用 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(sizeof(int))];
| ^~~~~~~~~~
我知道CMSG_SPACE
定义于sys/socket.h
as:
/* Amount of space + padding needed for a message of length l */
#define CMSG_SPACE(l) \
((unsigned int)_CMSG_HDR_ALIGN(sizeof (struct cmsghdr) + (l)))
但是,我不明白转换发生在哪里以及如何解决。谷歌没有任何帮助。
EDIT根据评论中的要求,以下是头文件中的更多信息:
#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))
The -8
来自~(_CMSG_HDR_ALIGNMENT - 1)
,以及long unsigned int
转换来自uintptr_t
.
编译器对以下类型的转换发出警告-8
to uintptr_t
在应用之前&
操作员。
注意:答案来自@jxh留下的评论
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)