Fwd: strict aliasing and socket structures

Abhinav Gauniyal mail.agauniyal@gmail.com
Mon Feb 20 17:33:00 GMT 2017


>How, exactly, did you discover this?  Do you know that the types
>are not compatible?  if so, please tell us precisely what the types
>are.

>Andrew.

You can try compiling any related code and it will give warnings like

" warning: dereferencing type-punned pointer might break strict-aliasing
rules"

Here's a snippet -

#include <iostream>
#include <cstring>
#include <netinet/in.h>

using namespace std;

int main(){

    sockaddr_storage addrStruct;
    memset(&addrStruct, 0, sizeof(addrStruct));

    sockaddr_in * tmp =
            reinterpret_cast<struct sockaddr_in *> (&addrStruct);
    tmp->sin_family = AF_INET;
}

It's in c++ but not too hard to imagine in C as well.
Here is online execution - http://melpon.org/wandbox/permlink/Y7IgD6Z8ha7VnvKA
Same warning as I said before.

You can try with any type you want, sockaddr_storage, _in, _in6,
_un etc and it shows these warnings. My original mail shows SO
links where I asked for these warnings and replies mentioned strict
alias violation.



More information about the Gcc-help mailing list