Remix.run Logo
trealira a day ago

From a draft of the C23 standard, this is what it has to say about union type punning:

> 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). This might be a non-value representation.

In past standards, it said "trap representation" rather than "non-value representation," but in none of them did it say that union type punning was undefined behavior. If you have a PDF of any standard or draft standard, just doing a search for "type punning" should direct you to this footnote quickly.

So I'm going to say that if the GCC developer explicitly said that union type punning was undefined behavior in C, then they were wrong, because that's not what the C standard says.

amboar a day ago | parent | next [-]

Section J.1 _Unspecified_ behavior says

> (11) The values of bytes that correspond to union members other than the one last stored into (6.2.6.1).

So it's a little more constrained in the ramifications, but the outcomes may still be surprising. It's a bit unfortunate that "UB" aliases to both "Undefined behavior" and "Unspecified behavior" given they have subtly different definitions.

From section 4 we have:

> A program that is correct in all other aspects, operating on correct data, containing unspecified behavior shall be a correct program and act in accordance with 5.1.2.4.

ryao a day ago | parent | prev [-]

Here is what was said:

> Type punning via unions is undefined behavior in both c and c++.

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=118141#c13

Feel free to start a discussion on the GCC mailing list.

trealira a day ago | parent [-]

I actually might, although not now. Thanks for the link. I'm surprised he directly contradicted the C standard, rather than it just being a misunderstanding.

ryao a day ago | parent [-]

According to another comment, the C standard contradicts the C standard on this:

https://news.ycombinator.com/item?id=43794268

Taking snippets of the C standard out of context of the whole seems to result in misunderstandings on this.

trealira a day ago | parent [-]

It doesn't. That commenter is saying that in C99, it was unspecified behavior. Since C11 onward, it's been removed from the unspecified behavior annex and type punning is allowed, though it may generate a trap/non-value representation. It was never undefined behavior, which is different.

Edit: no, it's still in the unspecified behavior annex, that's my mistake. It's still not undefined, though.

ryao a day ago | parent [-]

Most of the C code I write is C99 code, so it is undefined behavior either way for me (if I care about compilers other than GCC and Clang).

That said, I am going to defer to the GCC developers on this since I do not have time to make sense of all versions of the C standard.

trealira a day ago | parent [-]

That's fair. In the end, what matters is how C is implemented in practice on the platforms your code targets, not what the C standard says.