<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[reinterpret_cast wann definiert?]]></title><description><![CDATA[<p>Hallöchen,</p>
<p>Ich habe schon ab und zu Code gesehen, der einen vector&lt;uint8_t&gt; als uint32_t* anspricht, um gewisse Operationen auf ganzen 32-Bit-Ints auszuführen. Das funktioniert in der Realität immer, aber darf man das auch strikt nach Standard gesehen?</p>
<p>Falls nicht, könnt ihr euch Fälle vorstellen, wo das nicht funktioniert?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/324471/reinterpret_cast-wann-definiert</link><generator>RSS for Node</generator><lastBuildDate>Tue, 14 Jul 2026 16:19:48 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/324471.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 18 Mar 2014 17:39:31 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to reinterpret_cast wann definiert? on Tue, 18 Mar 2014 17:39:31 GMT]]></title><description><![CDATA[<p>Hallöchen,</p>
<p>Ich habe schon ab und zu Code gesehen, der einen vector&lt;uint8_t&gt; als uint32_t* anspricht, um gewisse Operationen auf ganzen 32-Bit-Ints auszuführen. Das funktioniert in der Realität immer, aber darf man das auch strikt nach Standard gesehen?</p>
<p>Falls nicht, könnt ihr euch Fälle vorstellen, wo das nicht funktioniert?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2389600</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2389600</guid><dc:creator><![CDATA[Reininterpretiererverein]]></dc:creator><pubDate>Tue, 18 Mar 2014 17:39:31 GMT</pubDate></item><item><title><![CDATA[Reply to reinterpret_cast wann definiert? on Tue, 18 Mar 2014 18:05:54 GMT]]></title><description><![CDATA[<p>Reininterpretiererverein schrieb:</p>
<blockquote>
<p>Ich habe schon ab und zu Code gesehen, der einen vector&lt;uint8_t&gt; als uint32_t* anspricht, um gewisse Operationen auf ganzen 32-Bit-Ints auszuführen. Das funktioniert in der Realität immer,</p>
</blockquote>
<p>Das funktioniert nie! Du meinst vielleicht den &quot;Inhalt&quot;, also data oder die Adresse des ersten Elements.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2389604</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2389604</guid><dc:creator><![CDATA[manni66]]></dc:creator><pubDate>Tue, 18 Mar 2014 18:05:54 GMT</pubDate></item><item><title><![CDATA[Reply to reinterpret_cast wann definiert? on Tue, 18 Mar 2014 18:30:29 GMT]]></title><description><![CDATA[<p>Nein, da gibt's zwei wundertolle Regeln im Standard, &quot;strict aliasing&quot; und &quot;alignment&quot;. Das sind einmal:</p>
<p>3.10/10 schrieb:</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:<br />
— the dynamic type of the object,<br />
— a cv-qualified version of the dynamic type of the object,<br />
— a type similar (as defined in 4.4) to the dynamic type of the object,<br />
— a type that is the signed or unsigned type corresponding to the dynamic type of the object,<br />
— a type that is the signed or unsigned type corresponding to a cv-qualified ersion of the dynamic type of the object,<br />
— an aggregate or union type that includes one of the aforementioned types among its elements or nonstatic data members (including, recursively, an element or non-static data member of a subaggregate or contained union),<br />
— a type that is a (possibly cv-qualified) base class type of the dynamic type of the object,<br />
— a char or unsigned char type.</p>
</blockquote>
<p>und einmal 3.11. Und diese Regeln sind durchaus auch nicht zu misachten, &quot;das funktioniert in der Realität immer&quot; ist nur für x86 und VS klar. Andere Prozessoren können u.U. gar keine 32 bit words aus nicht durch 4 teilbaren Adressen lesen (uint8_t's können aber auf solchen Adressen liegen), und andere Compiler optimieren vielleicht unter Annahme von strict aliasing.</p>
<p>Es gibt allerdings eine einfache Möglichkeit uint32_t's portabel aus Bytes zu lesen, die da wäre:</p>
<pre><code>uint8_t buf[4] = {0, 1, 2, 4};
uint32_t i;
memcpy(&amp;i, buf, sizeof i);
</code></pre>
<p>Das ist auch nicht langsamer, das memcpy wird komplett wegoptimiert und dieser Code erzeugt das gleiche binary:</p>
<pre><code>uint8_t buf[4] = {0, 1, 2, 4};
uint32_t i = *reinterpret_cast&lt;uint32_t*&gt;(buf);
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2389606</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2389606</guid><dc:creator><![CDATA[cooky451]]></dc:creator><pubDate>Tue, 18 Mar 2014 18:30:29 GMT</pubDate></item><item><title><![CDATA[Reply to reinterpret_cast wann definiert? on Tue, 18 Mar 2014 18:47:08 GMT]]></title><description><![CDATA[<blockquote>
<p>die da wäre:</p>
</blockquote>
<p>Wo steht, dass man einfach irgendein <code>char</code> -Array <strong>rein</strong>kopieren darf? Es ist doch gegeben, dass man zuerst das Objekt in das <code>char</code> -Array kopiert, und dann erst das <code>char</code> -Array zurück in das Objekt kopieren darf.</p>
<p>Oder irre ich mich da?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2389610</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2389610</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Tue, 18 Mar 2014 18:47:08 GMT</pubDate></item><item><title><![CDATA[Reply to reinterpret_cast wann definiert? on Tue, 18 Mar 2014 20:38:31 GMT]]></title><description><![CDATA[<p>Arcoth schrieb:</p>
<blockquote>
<blockquote>
<p>die da wäre:</p>
</blockquote>
<p>Wo steht, dass man einfach irgendein <code>char</code> -Array <strong>rein</strong>kopieren darf? Es ist doch gegeben, dass man zuerst das Objekt in das <code>char</code> -Array kopiert, und dann erst das <code>char</code> -Array zurück in das Objekt kopieren darf.</p>
<p>Oder irre ich mich da?</p>
</blockquote>
<p>Man darf alle PODs <code>char</code> -weise manipulieren wie man lustig ist. Nur das exakte Ergebnis ist implementationsdefiniert. Deswegen ist das selten eine gute Idee, aber verboten ist es nicht.</p>
<p>Kann sein, dass das Kopieren von ungültigen Zeigern, die auf diese Weise erzeugt wurden, UB ist.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2389632</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2389632</guid><dc:creator><![CDATA[TyRoXx]]></dc:creator><pubDate>Tue, 18 Mar 2014 20:38:31 GMT</pubDate></item><item><title><![CDATA[Reply to reinterpret_cast wann definiert? on Tue, 18 Mar 2014 21:07:01 GMT]]></title><description><![CDATA[<p>@Arcoth<br />
Ich bin mir zu 99% sicher dass das so wie cooky451 es geschrieben hat OK ist. Warum sollte es das nicht sein? Ich darf ja sogar ein beliebiges Objekt (beliebiger Typ!) jederzeit mittels einen char-Zeigers überschreiben.</p>
<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/25760">@cooky451</a><br />
So lange <code>uint8_t</code> als <code>char</code> oder <code>unsigned char</code> definiert ist, ist strict aliasing kein Thema. EDIT: &lt;== Unsinn /EDIT</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2389633</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2389633</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Tue, 18 Mar 2014 21:07:01 GMT</pubDate></item><item><title><![CDATA[Reply to reinterpret_cast wann definiert? on Tue, 18 Mar 2014 20:46:31 GMT]]></title><description><![CDATA[<p>hustbaer schrieb:</p>
<blockquote>
<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/25760">@cooky451</a><br />
So lange <code>uint8_t</code> als <code>char</code> oder <code>unsigned char</code> definiert ist, ist strict aliasing kein Thema.</p>
</blockquote>
<p>Ne, das ist nur in die eine Richtung der Fall. Hätte der TE geschrieben dass sie auf einen vector&lt;uint32_t&gt; mit uint8_t* zugreifen, dann wäre strict aliasing kein Problem (alignment natürlich auch nicht), anders rum aber sehr wohl.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2389637</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2389637</guid><dc:creator><![CDATA[cooky451]]></dc:creator><pubDate>Tue, 18 Mar 2014 20:46:31 GMT</pubDate></item><item><title><![CDATA[Reply to reinterpret_cast wann definiert? on Tue, 18 Mar 2014 21:06:16 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/25760">@cooky451</a><br />
Wie willst du die beiden Richtungen unterscheiden?</p>
<p>EDIT: OK, vergiss die Frage.<br />
Ja, als <code>uint32_t</code> lesen könnte problematisch werden. Schreiben müsste mMn. OK sein.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2389642</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2389642</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Tue, 18 Mar 2014 21:06:16 GMT</pubDate></item></channel></rss>