问题描述
请参阅 http://pubs.opengroup.org/onlinepubs/009696699/basedefs /sys/socket.h.html
( http://pubs.opengroup.org/onlinepubs/9699919799 来自第7期-从2013年开始,还是一样!)
(http://pubs.opengroup.org/onlinepubs/9699919799 is from Issue 7 - from 2013 and still the same!)
sockaddr_storage应该转换为其他结构类型,但这违反了ANSI和ISO C标准的别名规则据我所知. (对象可能无法通过指向不兼容类型的指针,除了可以通过3种字符类型进行访问,并且该结构及其第一个成员可以互换.)
sockaddr_storage is meant to be cast to other structure types,but that contradicts the ANSI and ISO C standards aliasing rulesas far as I can tell. (Objects may not be accessed throughpointers to incompatible types, with the exception that anythingcan be accessed through the 3 char types and that the structureand its first member are interchangeable.)
我知道使用套接字的实践存在很长时间在C标准化之前,但是POSIX应该符合ISO C实际上与它的手册中的标准相抵触. (即使在较新版本的POSIX.)
I know that that practice of working with sockets existed longbefore C was standardised, but POSIX is supposed to conform toISO C and actually it contradicts the standards in its manual. (Even in thenewer versions of POSIX.)
为什么他们首先要做到这一点?他们为什么不更改它?
Why did they make it like this in the first place?Why didn't they change it?
推荐答案
标准中的严格混叠规则限制了用户代码,而不是实现代码.由于POSIX标头和库是实现的一部分,因此POSIX和C标准之间没有实际冲突.
The strict-aliasing rules in the standard constrain user code, not implementation code. Since the POSIX headers and libraries are part of the implementation, there is no actual conflict between the POSIX and the C standard.
在开放源代码平台中,尤其是在Linux中,C库和编译器由不同的团队开发,这给实现者带来了麻烦,但这是他们的事,而不是您的事.例如,实现者可以:
In an open-source platform, and in particular in Linux where the C library and compiler are developed by different teams, this makes life difficult for implementors, but that is their concern, not yours. For example, the implementors could:
- 避免暴露标准之间的潜在冲突(即,禁用严格混叠优化);
- 承认其实现不符合POSIX(例如,没有POSIX认证的Linux发行版); 提供设施,以确保潜在冲突的设施实际上不发生冲突.从C标准的角度来看,这将是一个扩展.
- refrain from exposing the potential conflict between the standards (that is, disable strict-aliasing optimizations);
- admit that their implementation is not POSIX compliant (and note that, for example, there are no POSIX-certified Linux distributions);
- provide facilities to ensure that the potentially conflicting facilities do not actually conflict. From the point of view of the C standard, this would be an extension.
最后一个选项是gcc和glibc团队如何解决sockaddr问题;参见 https://gcc.gnu.org/bugzilla/show_bug.cgi?id= 71255
This last option is how the gcc and glibc teams are working to resolve the sockaddr issue; see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71255
这篇关于为什么POSIX违反ISO C标准的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!