Berkley Sockets, breaking aliasing rules?

2019-03-30 13:35发布

问题:

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:

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.