Re: type-punned pointers!? (154 Views)
Reply
Occasional Contributor
Henrik Goldman
Posts: 4
Registered: ‎06-09-2006
Message 1 of 3 (154 Views)

type-punned pointers!?

Hi,

I'm seeing a weird problem on hp-ux (ia64) using g++ 4.1.1.


Here below is a tiny bit of code which sets up the nessecary code for doing
a select():


fd_set fdread, fdwrite;


FD_ZERO(&fdread);


FD_ZERO(&fdwrite);


FD_SET(m_IncomingSocket.GetSocket(), &fdread);


FD_SET(m_IncomingSocket.GetSocket(), &fdwrite);


For the last two lines I get the following warning:


warning: dereferencing type-punned pointer will break strict-aliasing rules
warning: dereferencing type-punned pointer will break strict-aliasing rules


I don't really understand why 'clean' code would give such a warning since
it's structures which are defined by the system.
However perhaps someone could tell me what it actually means and eventually
how to solve it? My g++ optimization is O2.


Thanks in advance.
-- Henrik
Acclaimed Contributor
Dennis Handly
Posts: 25,283
Registered: ‎03-06-2006
Message 2 of 3 (154 Views)

Re: type-punned pointers!?

>I don't really understand why 'clean' code would give such a warning since it's structures which are defined by the system.

The trouble is that the system headers think they can do anything but don't realize they break strict ANSI C rules. ;-)

You probably can ignore this. I don't know if there is a g++ option to suppress this. Or if the warning will tell g++ not to do that aliasing.
Occasional Contributor
Henrik Goldman
Posts: 4
Registered: ‎06-09-2006
Message 3 of 3 (154 Views)

Re: type-punned pointers!?

Yes it seems it's a bug in the system headers.

For now I just fixed the header file /usr/include/sys/_fd_macros.h:

# define FD_SET(n,p) (((__fd_mask *)(void*)((p)->fds_bits))[(n)/_NFDBITS] |...
# define FD_CLR(n,p) (((__fd_mask *)(void*)((p)->fds_bits))[(n)/_NFDBITS] &=...
# define FD_ISSET(n,p) (((__fd_mask *)(void*)((p)->fds_bits))[(n)/_NFDBITS] ...


I have added "(void *)" on all 3 lines. I think it's because the cast is unsafe that I get the complain. On other platforms I'm sure the same thing would have issued "bus errors" which I have experienced in past.

-- Henrik
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.