LWN.net Logo

Yes, that's true...

Yes, that's true...

Posted May 25, 2011 5:01 UTC (Wed) by khim (subscriber, #9252)
In reply to: Oh, forgot to say... by jrn
Parent article: What Every C Programmer Should Know About Undefined Behavior #3/3

Ah, my bad. Sure, but the temptation is very high to use conversion to int because they are of the same size - and this is impossible to do even if you use two transitions like (int *)(char *)pf.

The only way to portably and correctly do that is via memcpy - and with current crop of the compilers it's quite efficient too (both memcpy and pointers will be elided), but this is counter-intuitive if you don't know about undefined behaviors and still think that C is high-level assembler.


(Log in to post comments)

Yes, that's true...

Posted May 25, 2011 5:19 UTC (Wed) by jrn (subscriber, #64214) [Link]

Even memcpy is not portable to platforms where sizeof(float) != sizeof(int). :)

A rough and incomplete rule of thumb about aliasing rules is that constructs that would be portable given how alignment and size varies from platform to platform are likely to be permitted.

Copyright © 2013, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds