Hacker News new | ask | show | jobs
by mistercow 5018 days ago
Yep, type punning through a union is nonstandard and unportable, but if I'm not mistaken, it is a documented feature in GCC.
1 comments

Type punning through a union is explicitly defined to work in C99 and C11 (footnote 95 in C11):

> If the member used to read the contents of a union object is not the same as the member last used to store a value in the object, the appropriate part of the object representation of the value is reinterpreted as an object representation in the new type as described in 6.2.6 (a process sometimes called ‘‘type punning’’).

Please don't help spread the myth that compiler writers can break this idiom. That said, I use memcpy in my own code.

All right, I see it in C99 as well now (§6.5.2.3 footnote 82, if I'm not mistaken). Thanks for the correction.
Oh, well I stand corrected.