<?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 vs. C Casts]]></title><description><![CDATA[<p>Moinsen</p>
<p>Heute habe ich etwas &quot;interessantes&quot; gesehen. Irgendwie Funktioniert<br />
der reinterpret_cast bei VC++-9 nicht richtig?</p>
<p>Ich habe Folgende Funktion, welche mir aus dem struct den String<br />
Extrahiert. Es ist so eine MS Struktur: <a href="http://msdn.microsoft.com/en-us/library/ms735602(VS.85).aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/ms735602(VS.85).aspx</a></p>
<pre><code class="language-cpp">std::string CUtils::extractString(char* strPoint, DWORD &amp;size)
  {
    char* tmpString = reinterpret_cast&lt;char*&gt;(malloc(size +1));

    memset(tmpString, 0, size +1);
    memcpy(tmpString, strPoint, size);

    tmpString[size] = '\0';
    std::string retStr(tmpString);

    free(tmpString);
    return retStr;
  }
</code></pre>
<p>Beim Aufruf kommt aber nun das Komische:</p>
<p><strong>Funktioniert nicht (C++)</strong>:</p>
<pre><code class="language-cpp">m_devList.at(i) = CUtils::extractString(
            reinterpret_cast&lt;char*&gt;(devCap + devCap-&gt;dwLineNameOffset),
            devCap-&gt;dwLineNameSize);
</code></pre>
<p><strong>Funktioniert (C):</strong></p>
<pre><code class="language-cpp">m_devList.at(i) = CUtils::extractString(
            (char*)devCap + devCap-&gt;dwLineNameOffset,
            devCap-&gt;dwLineNameSize);
</code></pre>
<p>reinterpret_cast&lt;char*&gt; sollte doch das gleiche machen wie (char*)?!<br />
Hab ich da was falsches im Kopf?</p>
<p>Gruss<br />
Ghost</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/223956/reinterpret_cast-vs-c-casts</link><generator>RSS for Node</generator><lastBuildDate>Sat, 03 Oct 2026 12:53:27 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/223956.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 03 Oct 2008 10:07:16 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to reinterpret_cast vs. C Casts on Fri, 03 Oct 2008 10:07:16 GMT]]></title><description><![CDATA[<p>Moinsen</p>
<p>Heute habe ich etwas &quot;interessantes&quot; gesehen. Irgendwie Funktioniert<br />
der reinterpret_cast bei VC++-9 nicht richtig?</p>
<p>Ich habe Folgende Funktion, welche mir aus dem struct den String<br />
Extrahiert. Es ist so eine MS Struktur: <a href="http://msdn.microsoft.com/en-us/library/ms735602(VS.85).aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/ms735602(VS.85).aspx</a></p>
<pre><code class="language-cpp">std::string CUtils::extractString(char* strPoint, DWORD &amp;size)
  {
    char* tmpString = reinterpret_cast&lt;char*&gt;(malloc(size +1));

    memset(tmpString, 0, size +1);
    memcpy(tmpString, strPoint, size);

    tmpString[size] = '\0';
    std::string retStr(tmpString);

    free(tmpString);
    return retStr;
  }
</code></pre>
<p>Beim Aufruf kommt aber nun das Komische:</p>
<p><strong>Funktioniert nicht (C++)</strong>:</p>
<pre><code class="language-cpp">m_devList.at(i) = CUtils::extractString(
            reinterpret_cast&lt;char*&gt;(devCap + devCap-&gt;dwLineNameOffset),
            devCap-&gt;dwLineNameSize);
</code></pre>
<p><strong>Funktioniert (C):</strong></p>
<pre><code class="language-cpp">m_devList.at(i) = CUtils::extractString(
            (char*)devCap + devCap-&gt;dwLineNameOffset,
            devCap-&gt;dwLineNameSize);
</code></pre>
<p>reinterpret_cast&lt;char*&gt; sollte doch das gleiche machen wie (char*)?!<br />
Hab ich da was falsches im Kopf?</p>
<p>Gruss<br />
Ghost</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1592816</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1592816</guid><dc:creator><![CDATA[xGhost]]></dc:creator><pubDate>Fri, 03 Oct 2008 10:07:16 GMT</pubDate></item><item><title><![CDATA[Reply to reinterpret_cast vs. C Casts on Fri, 03 Oct 2008 10:15:07 GMT]]></title><description><![CDATA[<p>Beim C-Cast besteht z.B. die Gefahr, dass du aus versehen die constness wegcastest.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1592821</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1592821</guid><dc:creator><![CDATA[__Stefan__]]></dc:creator><pubDate>Fri, 03 Oct 2008 10:15:07 GMT</pubDate></item><item><title><![CDATA[Reply to reinterpret_cast vs. C Casts on Fri, 03 Oct 2008 10:27:10 GMT]]></title><description><![CDATA[<p>xGhost schrieb:</p>
<blockquote>
<p>reinterpret_cast&lt;char*&gt; sollte doch das gleiche machen wie (char*)?!<br />
Hab ich da was falsches im Kopf?</p>
</blockquote>
<p>Achte auf die Klammersetzung. Die beiden Codefragmente sind unabhängig vom cast nicht identisch.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1592830</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1592830</guid><dc:creator><![CDATA[()]]></dc:creator><pubDate>Fri, 03 Oct 2008 10:27:10 GMT</pubDate></item><item><title><![CDATA[Reply to reinterpret_cast vs. C Casts on Fri, 03 Oct 2008 11:14:53 GMT]]></title><description><![CDATA[<p>() schrieb:</p>
<blockquote>
<p>xGhost schrieb:</p>
<blockquote>
<p>reinterpret_cast&lt;char*&gt; sollte doch das gleiche machen wie (char*)?!<br />
Hab ich da was falsches im Kopf?</p>
</blockquote>
<p>Achte auf die Klammersetzung. Die beiden Codefragmente sind unabhängig vom cast nicht identisch.</p>
</blockquote>
<p>Wie meinst du das?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1592853</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1592853</guid><dc:creator><![CDATA[xGhost]]></dc:creator><pubDate>Fri, 03 Oct 2008 11:14:53 GMT</pubDate></item><item><title><![CDATA[Reply to reinterpret_cast vs. C Casts on Fri, 03 Oct 2008 11:27:14 GMT]]></title><description><![CDATA[<p>xGhost schrieb:</p>
<blockquote>
<p>Wie meinst du das?</p>
</blockquote>
<p>Operatoren haben eine gewisse Rangfolge. Die Typumwandlung steht dabei vor der Addition.</p>
<p>Hier wird zuerst addiert und anschließend gecastet</p>
<pre><code class="language-cpp">reinterpret_cast&lt;char*&gt;(devCap + devCap-&gt;dwLineNameOffset)
</code></pre>
<p>Hier wird der 1. Operand der Addition gecastet <strong>bevor</strong> die Addition durchgeführt wird</p>
<pre><code class="language-cpp">(char*)devCap + devCap-&gt;dwLineNameOffset
</code></pre>
<p>Es passiert also logisch nicht dasselbe.</p>
<p>Wenn du den 1. Code in die Form des 2. bringen willst muss die Klammerung anders gemacht werden.</p>
<pre><code class="language-cpp">reinterpret_cast&lt;char*&gt;(devCap) + devCap-&gt;dwLineNameOffset
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1592860</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1592860</guid><dc:creator><![CDATA[()]]></dc:creator><pubDate>Fri, 03 Oct 2008 11:27:14 GMT</pubDate></item><item><title><![CDATA[Reply to reinterpret_cast vs. C Casts on Fri, 03 Oct 2008 11:28:47 GMT]]></title><description><![CDATA[<p>er meint damit, dass die beiden codefragmente sich unterscheiden. das eine castet (pointer+offset), das andere castet pointer und addiert dann erst den offset. das ist ein großer unterschied.</p>
<p>unabhängig davon ist extractString ein ziemlich mieses stück code. hast du die aus den 80ern mitgebracht?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1592862</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1592862</guid><dc:creator><![CDATA[bad code day]]></dc:creator><pubDate>Fri, 03 Oct 2008 11:28:47 GMT</pubDate></item><item><title><![CDATA[Reply to reinterpret_cast vs. C Casts on Fri, 03 Oct 2008 12:10:00 GMT]]></title><description><![CDATA[<p>() schrieb:</p>
<blockquote>
<p>Operatoren haben eine gewisse Rangfolge.</p>
</blockquote>
<p>Das hat mir im ersten Post gefehlt. Das habe ich vergessen.</p>
<blockquote>
<p>unabhängig davon ist extractString ein ziemlich mieses stück code. hast du die aus den 80ern mitgebracht?</p>
</blockquote>
<p>Meine Erste Variante war:</p>
<pre><code class="language-cpp">std::string(reinterpret_cast&lt;char*&gt;(devCap + devCap-&gt;dwLineNameOffset), devCap-&gt;dwLineNameSize);
/* Korrektur */
std::string(reinterpret_cast&lt;char*&gt;(devCap) + devCap-&gt;dwLineNameOffset, devCap-&gt;dwLineNameSize);
</code></pre>
<p>Die werde ich wohl wieder nehmen können <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
<p>peace</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1592895</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1592895</guid><dc:creator><![CDATA[xGhost]]></dc:creator><pubDate>Fri, 03 Oct 2008 12:10:00 GMT</pubDate></item><item><title><![CDATA[Reply to reinterpret_cast vs. C Casts on Fri, 03 Oct 2008 13:14:53 GMT]]></title><description><![CDATA[<p>Ich will mal gar nicht wissen, was devCap ist... wtfs sterben eben nicht aus.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1592935</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1592935</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Fri, 03 Oct 2008 13:14:53 GMT</pubDate></item><item><title><![CDATA[Reply to reinterpret_cast vs. C Casts on Fri, 03 Oct 2008 14:37:27 GMT]]></title><description><![CDATA[<p>camper schrieb:</p>
<blockquote>
<p>Ich will mal gar nicht wissen, was devCap ist... wtfs sterben eben nicht aus.</p>
</blockquote>
<p>Im Ersten Post auf den Link klicken ...<br />
Man muss tun was man für seinen Lohn halt tun muss...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1592991</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1592991</guid><dc:creator><![CDATA[xGhost]]></dc:creator><pubDate>Fri, 03 Oct 2008 14:37:27 GMT</pubDate></item><item><title><![CDATA[Reply to reinterpret_cast vs. C Casts on Fri, 03 Oct 2008 14:52:00 GMT]]></title><description><![CDATA[<p>xGhost schrieb:</p>
<blockquote>
<p>Man muss tun was man für seinen Lohn halt tun muss...</p>
</blockquote>
<p>Stimmt, wir werden ja für unsere Antworten bezahlt... <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f644.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_rolling_eyes"
      title=":rolling_eyes:"
      alt="🙄"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1593000</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1593000</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Fri, 03 Oct 2008 14:52:00 GMT</pubDate></item></channel></rss>