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.
精彩评论