Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>No, the code that you have posted does not exhibit <em>undefined behavior</em> but attempting to read <code>*p</code> would. Also, depending on the alignment requirements of <code>int</code> and <code>short</code>, the result of the cast may be unspecified and irreversable (see 5.2.10 [expr.reinterpret.cast] / 7).</p> <p>See ISO/IEC 14882:2011 3.10 [basic.lval] / 10:</p> <blockquote> <p>If a program attempts to access the stored value of an object through a glvalue of other than one of the following types the behavior is undefined:</p> <ul> <li>the dynamic type of the object,</li> <li>a cv-qualified version of the dynamic type of the object,</li> <li>a type similar (as defined in 4.4) to the dynamic type of the object,</li> <li>a type that is the signed or unsigned type corresponding to the dynamic type of the object,</li> <li>a type that is the signed or unsigned type corresponding to a cv-qualified version of the dynamic type of the object,</li> <li>an aggregate or union type that includes one of the aforementioned types among its elements or non-static data members (including, recursively, an element or non-static data member of a subaggregate or contained union),</li> <li>a type that is a (possibly cv-qualified) base class type of the dynamic type of the object,</li> <li>a char or unsigned char type.</li> </ul> </blockquote> <p>The object that you are trying to access is a <code>short</code> and <code>*p</code> is a <em>glvalue</em> of type <code>int</code> which doesn't meet any of the above descriptions.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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