Berkley Sockets, breaking aliasing rules?

2019-03-30 13:47发布

Im asking my self, can i use the BSD sockets with strict aliasing on, without getting undefined behaviour by compiling with gcc?

bind(sdListen, (struct sockaddr*)&sockaddr_inIdentifier, sizeof(sockaddr_inIdentifier))

This line of code breaks the strict aliasing rule as far as i know (and gcc gives me the same warning). so is there a plan b, of using the sockets in O3 mode without turning strictaliasing of? And of course without breaking the rule? or do i have to get an own socket system running that will be runnable on all systems/compilers?

1条回答
Root(大扎)
2楼-- · 2019-03-30 14:12

The cast itself in that line does not break the strict aliasing rule. The rule is only broken if the implementation of bind() dereferences that pointer without converting it back to the right type.

Any strict aliasing problems there are problems for the implementer of bind(), not the user.

查看更多
登录 后发表回答