Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Direct memory reinterpretation, by definition, means working with lvalues. The most straightforward approach would be to do it though a cast to reference type</p> <pre><code>double d = reinterpret_cast&lt;double &amp;&gt;(voidp); </code></pre> <p>You can also do it through a pointer cast, as other answers suggested, although it "overloads" the procedure with a number of completely unnecessary operator applications. Both approaches are equivalent, since by definition <code>reinterpret_cast</code> to reference type <code>reinterpret_cast&lt;T &amp;&gt;(v)</code> is equivalent to the pointer version <code>*reinterpret_cast&lt;T *&gt;(&amp;v)</code>.</p> <p>However, the above approaches suffer from type-punning issues. Formally, doing this is simply illegal. You are not allowed to read <code>void *</code> objects as <code>double</code> objects in C++. Direct memory reinterpretation exists in C++ for re-interpreting objects as arrays of <code>char</code>s, not for arbitrary type-punning like the above. Even if we ignore the formal issue and stick to purely "practical" considerations, trying to directly reinterpret a <code>void *</code> value as <code>double</code> value might produce completely unexpected and meaningless results in a compiler that follows strict-aliasing semantics when performing optimizations.</p> <p>A better idea might be to <code>memcpy</code> the <code>void *</code> object to the <code>double</code> object</p> <pre><code>double d; assert(sizeof d == sizeof voidp); // &lt;- a static assert would be even better memcpy(&amp;d, &amp;voidp, sizeof d); </code></pre> <p>Alternatively, in C you are now allowed to use unions for that purpose. I'm not sure the formal permission made into C++ yet, but it will typically work in practice.</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