This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Fwd: strict aliasing and socket structures


>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.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]