Note that there are some explanatory texts on larger screens.

plurals
  1. POIs type-punning through a union unspecified in C99, and has it become specified in C11?
    primarykey
    data
    text
    <p>A number of answers for the Stack Overflow question <a href="https://stackoverflow.com/questions/11638091/getting-the-ieee-single-precision-bits-for-a-float">Getting the IEEE Single-precision bits for a float</a> suggest using a <code>union</code> structure for type punning (e.g.: turning the bits of a <code>float</code> into a <code>uint32_t</code>):</p> <pre><code>union { float f; uint32_t u; } un; un.f = your_float; uint32_t target = un.u; </code></pre> <p>However, the value of the <code>uint32_t</code> member of the union appears to be unspecified according to the C99 standard (at least draft n1124), where section 6.2.6.1.7 states: </p> <blockquote> <p>When a value is stored in a member of an object of union type, the bytes of the object representation that do not correspond to that member but do correspond to other members take unspecified values.</p> </blockquote> <p>At least one footnote of the C11 n1570 draft seems to imply that this is no longer the case (see footnote 95 in 6.5.2.3):</p> <blockquote> <p>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.</p> </blockquote> <p>However, the text to section 6.2.6.1.7 is the same in the C99 draft as in the C11 draft.</p> <p>Is this behavior actually unspecified under C99? Has it become specified in C11? I realize that most compilers seem to support this, but it would be nice to know if it's specified in the standard, or just a very common extension.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. COTechnical note: Accessing a union member other than the last one stored does not cause a program to violate the C standard. Accessing such a union member results in an unspecified value (not undefined behavior), and, per C 1999 4 3, “shall be a correct program and act in accordance with 5.1.2.3.” Further, a compiler may provide additional guarantees about the value and remain a conforming implementation.
      singulars
    2. COBasically what Wug said. The change is that C99 nowhere explicitly mentions that reading members other than the one last written to is okay, while C11 (at least the draft n1570) does. So by "Undefined behavior is otherwise indicated in this International Standard by the words ‘‘undefined behavior’’ _or by the omission of any explicit definition of behavior_." it was sometimes stated that that was undefined behaviour. I'm not enough of a language lawyer to cast a definitive verdict on that interpretation.
      singulars
    3. COThis is not a recent addition, but already appears in n1256. And this had been a modification as a result of a defect report: the intention had always been the one that is expressed now.
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload