Is writing then reading different union members undefined according to the C standard? [duplicate]

  • A+
Category:Languages

I read that this code it is undefined according to the c standard but I cant find why. It is compiles without errors in gcc 8.1.0 and clang-6.0 and prints 1.

the code is as follows:

#include <stdio.h>  int main() {    union {      int i;      short s;    } u;      u.i = 42;    u.s = 1;     printf("%d/n", u.i);    return 0; } 

 


From the C11 specification, §6.5.2.3 note 95:

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 trap representation.

This says that what you're doing is allowed, but also implies that the value you read may not be what you expect (for example by writing to an int member and reading from a float member).

There's also the caveat about trap representation values in which case the behavior will be undefined. For two's complement systems (which is the vast majority of all computers the last couple of decades) this isn't an issue with integer values though.


In your case the result will depends very much on the platforms endianness. Either you will get the value you write (1) or you will get 0.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: