This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Fwd: strict aliasing and socket structures
- From: Abhinav Gauniyal <mail dot agauniyal at gmail dot com>
- To: gcc-help at gcc dot gnu dot org
- Date: Mon, 20 Feb 2017 23:02:49 +0530
- Subject: Fwd: strict aliasing and socket structures
- Authentication-results: sourceware.org; auth=none
- References: <CALd8CEuhwQoK_LZZwKjz7nSkStPbHVdcfF0uMuREEi3BEzr9tw@mail.gmail.com>
>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.