开发者

C, Unix Domain Sockets, Ancillary data, and GCC; Using CMSG_DATA macro

How can I do this:

*(int *)CMSG_DATA(hdr) = fd2pass;

Without GCC raising this:

error: dereferencing type-punned pointer will break stri开发者_如何学编程ct-aliasing rules

In a way compatible with these options:

-Wall -Werror -pedantic


Unless something is very wrong, there is no actual aliasing going on -- the object referred to by *(int *)CMSG_DATA(hdr) is not an alias for hdr -- it's past the end of hdr. The warning is incorrect.

You can work around it with memcpy:

memcpy(CMSG_DATA(hdr), &fd2pass, sizeof(int));

Don't use -fno-strict-aliasing: that disables optimizations that assume strict aliasing; it could generate considerably worse code.

For technical details, see glibc bug 16197.


Try passing -fno-strict-aliasing to gcc.

To shed a light on the strict aliasing topic, check this question.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜