<?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[Frage zu static_cast]]></title><description><![CDATA[<p>Hallo</p>
<p>Bin gerade dabei mich in C / C++ einzuarbeiten und beschäftige mich gerade mit Zeigeraritmetik.</p>
<p>Dabei bin ich auf folgenden Code gestoßen. Wobei ich nicht ganz begreife wieso hier die Typenkonvertierung nicht geht. Sagt mein Compiler (Visaul Studio 2010)<br />
jedenfals.</p>
<pre><code class="language-cpp">double d[10] = {1,2,3,4,5,6,7,8,9,20};

double *dp1 = d;
double *dp2;

dp2 = dp1 + 1;

cout &lt;&lt; static_cast&lt;long&gt;(dp2) - static_cast&lt;long&gt;(dp1) &lt;&lt; endl; // Hier gibts Ärger
</code></pre>
<p>Vieleicht weiß ja jemand Rat. Das Beispiel ist aus dem Buch C++ Einführung und Professionelle Programmierung von Ulrich Breymann</p>
<p>Schon mal Danke für die Hilfe</p>
<p>mfg<br />
Fujittsu</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/282187/frage-zu-static_cast</link><generator>RSS for Node</generator><lastBuildDate>Sun, 23 Aug 2026 08:56:32 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/282187.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 15 Feb 2011 16:46:44 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Frage zu static_cast on Tue, 15 Feb 2011 16:46:44 GMT]]></title><description><![CDATA[<p>Hallo</p>
<p>Bin gerade dabei mich in C / C++ einzuarbeiten und beschäftige mich gerade mit Zeigeraritmetik.</p>
<p>Dabei bin ich auf folgenden Code gestoßen. Wobei ich nicht ganz begreife wieso hier die Typenkonvertierung nicht geht. Sagt mein Compiler (Visaul Studio 2010)<br />
jedenfals.</p>
<pre><code class="language-cpp">double d[10] = {1,2,3,4,5,6,7,8,9,20};

double *dp1 = d;
double *dp2;

dp2 = dp1 + 1;

cout &lt;&lt; static_cast&lt;long&gt;(dp2) - static_cast&lt;long&gt;(dp1) &lt;&lt; endl; // Hier gibts Ärger
</code></pre>
<p>Vieleicht weiß ja jemand Rat. Das Beispiel ist aus dem Buch C++ Einführung und Professionelle Programmierung von Ulrich Breymann</p>
<p>Schon mal Danke für die Hilfe</p>
<p>mfg<br />
Fujittsu</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2021379</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2021379</guid><dc:creator><![CDATA[fujittsu]]></dc:creator><pubDate>Tue, 15 Feb 2011 16:46:44 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu static_cast on Tue, 15 Feb 2011 16:49:52 GMT]]></title><description><![CDATA[<p>static_cast ist für eine solche Konvertierung nicht geeignet, dazu brauchst du wenn dann reinterpret_cast. Allerdings kommst du bei dem Beispiel wohl sowieso ohne cast aus!?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2021381</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2021381</guid><dc:creator><![CDATA[dot]]></dc:creator><pubDate>Tue, 15 Feb 2011 16:49:52 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu static_cast on Tue, 15 Feb 2011 16:52:12 GMT]]></title><description><![CDATA[<p>dp1 und dp2 sind Pointer ... die du in ein long konvertierst? Versuch mal *dp1 und *dp2 ... also dereffen ... oder willst du Pointer voneinander abziehen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2021383</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2021383</guid><dc:creator><![CDATA[padreigh]]></dc:creator><pubDate>Tue, 15 Feb 2011 16:52:12 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu static_cast on Tue, 15 Feb 2011 16:53:07 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">cout &lt;&lt; static_cast&lt;long&gt;(*dp2) - static_cast&lt;long&gt;(*dp1) &lt;&lt; endl; // Etwas weniger Ärger
</code></pre>
<p>Braucht da aber eigentlich kein cast.</p>
<p>*Edit<br />
omg - mal wieder viel zu langsam -.-</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2021386</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2021386</guid><dc:creator><![CDATA[icarus2]]></dc:creator><pubDate>Tue, 15 Feb 2011 16:53:07 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu static_cast on Tue, 15 Feb 2011 16:58:53 GMT]]></title><description><![CDATA[<p>Da habe ich woll ein wenig dazu geschrieben. Es geht darum festzustellen wieviel<br />
Byte die beiden Zeiger auseinander liegen.</p>
<p>Das dürften nach meinen bescheiden Kenntnissen 8 Byte sein. Da eine double Variable ja 8 Byte belegt. Nun wollte ich das durch diese Rechnung verdeutlich.</p>
<p>mfg<br />
Fujittsu</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2021393</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2021393</guid><dc:creator><![CDATA[fujittsu]]></dc:creator><pubDate>Tue, 15 Feb 2011 16:58:53 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu static_cast on Tue, 15 Feb 2011 17:03:58 GMT]]></title><description><![CDATA[<p><code>&amp;(d[1])-&amp;(d[0])</code> ?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2021395</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2021395</guid><dc:creator><![CDATA[padreigh]]></dc:creator><pubDate>Tue, 15 Feb 2011 17:03:58 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu static_cast on Tue, 15 Feb 2011 17:05:27 GMT]]></title><description><![CDATA[<p>fujittsu schrieb:</p>
<blockquote>
<p>Es geht darum festzustellen wieviel<br />
Byte die beiden Zeiger auseinander liegen.</p>
</blockquote>
<pre><code class="language-cpp">(dp2 - dp1) * sizeof(double)
</code></pre>
<p>!?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2021398</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2021398</guid><dc:creator><![CDATA[dot]]></dc:creator><pubDate>Tue, 15 Feb 2011 17:05:27 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu static_cast on Tue, 15 Feb 2011 17:09:31 GMT]]></title><description><![CDATA[<p>Danke schön für eure Hilfe! Ich muss mir die Sache nochmal durch den Kopf<br />
gehen lassen.</p>
<p>mfg<br />
Fujittsu</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2021400</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2021400</guid><dc:creator><![CDATA[fujittsu]]></dc:creator><pubDate>Tue, 15 Feb 2011 17:09:31 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu static_cast on Tue, 15 Feb 2011 17:19:03 GMT]]></title><description><![CDATA[<p>dot schrieb:</p>
<blockquote>
<p>!?</p>
</blockquote>
<p>‽</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2021408</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2021408</guid><dc:creator><![CDATA[Interrobang]]></dc:creator><pubDate>Tue, 15 Feb 2011 17:19:03 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu static_cast on Tue, 15 Feb 2011 18:43:53 GMT]]></title><description><![CDATA[<p>fujittsu schrieb:</p>
<blockquote>
<pre><code class="language-cpp">double d[10] = {1,2,3,4,5,6,7,8,9,20};

double *dp1 = d;
double *dp2;

dp2 = dp1 + 1;

cout &lt;&lt; static_cast&lt;long&gt;(dp2) - static_cast&lt;long&gt;(dp1) &lt;&lt; endl;
</code></pre>
</blockquote>
<pre><code class="language-cpp">cout &lt;&lt; (dp2-dp1) &lt;&lt; endl;
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2021468</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2021468</guid><dc:creator><![CDATA[krümelkacker]]></dc:creator><pubDate>Tue, 15 Feb 2011 18:43:53 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu static_cast on Wed, 16 Feb 2011 09:18:06 GMT]]></title><description><![CDATA[<p>Eine Adresse ist eine Zahl. Von älteren Versionen des GCC-Compilers wurde <code>static_cast</code> akzeptiert, was jetzt nicht mehr der Fall ist. Sorry, mein Fehler, dass ich die Stelle nicht aktualisiert habe. <code>reinterpret_cast</code> verzichtet auf jegliche Typ-Verträglichkeitsprüfung und ist hier angebracht - oder man nimmt den alten C-Cast, der aber nicht mehr empfohlen wird. Ohne Cast geht es aber nicht, weil ja gerade gezeigt werden soll, dass die Adressen um 8 Byte auseinanderliegen, wenn nur ein Element im Feld weitergegangen wird.</p>
<pre><code class="language-cpp">cout &lt;&lt; dp2-dp1 &lt;&lt; endl;                  // ergibt 1

   cout &lt;&lt;  reinterpret_cast&lt;long&gt;(dp2)      // C++, ergibt 8
      - reinterpret_cast&lt;long&gt;(dp1) &lt;&lt; endl;

   cout &lt;&lt; (long)dp2 - (long)dp1 &lt;&lt; endl;    // C-cast, ergibt 8
</code></pre>
<p>U. B.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2021683</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2021683</guid><dc:creator><![CDATA[Breymann]]></dc:creator><pubDate>Wed, 16 Feb 2011 09:18:06 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu static_cast on Wed, 16 Feb 2011 10:04:27 GMT]]></title><description><![CDATA[<p>Danke für die Antwort. Da habe ich mir woll doch das richtige Buch gekauft wenn<br />
gleich der Autor antwortet.</p>
<p>mfg<br />
Fujittsu</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2021701</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2021701</guid><dc:creator><![CDATA[fujittsu]]></dc:creator><pubDate>Wed, 16 Feb 2011 10:04:27 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu static_cast on Wed, 16 Feb 2011 12:55:56 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">(dp2 - dp1) * sizeof(*dp1)
</code></pre>
<p>oder</p>
<pre><code class="language-cpp">reinterpret_cast&lt;char*&gt;(dp2) - reinterpret_cast&lt;char*&gt;(dp1)
</code></pre>
<p>Das andere ist... ein bisschen unglücklich.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2021768</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2021768</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Wed, 16 Feb 2011 12:55:56 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu static_cast on Wed, 16 Feb 2011 13:24:47 GMT]]></title><description><![CDATA[<blockquote>
<pre><code class="language-cpp">(dp2 - dp1) * sizeof(*dp1)
</code></pre>
</blockquote>
<p>zeigt nur, dass man annimmt, dass die Adressen tatsächlich <code>sizeof(double)</code> auseinanderliegen (ist ja eine vernünftige Annahme). Bewiesen wird es dadurch aber nicht. Dazu muss man schon die tatsächlichen Byte-Adressen ermitteln.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2021791</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2021791</guid><dc:creator><![CDATA[Breymann]]></dc:creator><pubDate>Wed, 16 Feb 2011 13:24:47 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu static_cast on Wed, 16 Feb 2011 14:03:12 GMT]]></title><description><![CDATA[<p>Breymann schrieb:</p>
<blockquote>
<blockquote>
<pre><code class="language-cpp">(dp2 - dp1) * sizeof(*dp1)
</code></pre>
</blockquote>
<p>zeigt nur, dass man annimmt, dass die Adressen tatsächlich <code>sizeof(double)</code> auseinanderliegen (ist ja eine vernünftige Annahme). Bewiesen wird es dadurch aber nicht. Dazu muss man schon die tatsächlichen Byte-Adressen ermitteln.</p>
</blockquote>
<p>Beweisen lässt sich durch Code sowieso nichts. Wenn die Annahme nicht zuträfe, wäre das Ergebnis undefiniertes Verhalten.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2021819</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2021819</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Wed, 16 Feb 2011 14:03:12 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu static_cast on Wed, 16 Feb 2011 14:34:14 GMT]]></title><description><![CDATA[<p>Wie zeigt man, wieviele <strong>Bytes</strong> zwischen 2 Adressen liegen? Indem man beide Adressen als Byte-Adresse bestimmt und sie voneinander abzieht. Sowas wie</p>
<pre><code class="language-cpp">(dp2-dp1)*sizeof(*dp1)
</code></pre>
<p>macht das nicht. Es ist eine blosse Multiplikation, die darüber nix aussagt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2021832</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2021832</guid><dc:creator><![CDATA[Besserwisser2]]></dc:creator><pubDate>Wed, 16 Feb 2011 14:34:14 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu static_cast on Wed, 16 Feb 2011 15:21:45 GMT]]></title><description><![CDATA[<p>Besserwisser2 schrieb:</p>
<blockquote>
<p>Wie zeigt man, wieviele <strong>Bytes</strong> zwischen 2 Adressen liegen? Indem man beide Adressen als Byte-Adresse bestimmt und sie voneinander abzieht. Sowas wie</p>
<pre><code class="language-cpp">(dp2-dp1)*sizeof(*dp1)
</code></pre>
<p>macht das nicht. Es ist eine blosse Multiplikation, die darüber nix aussagt.</p>
</blockquote>
<p>Ich sehe hier zunächst einmal eine Subtraktion von zwei Zeigern. Das Ergebnis ist wohldefiniert und es besteht ein bekannter (weil so festgelegter) Zusammenhang mit dem Abstand der Objekte ausgedrückt in Bytes. Ich verstehe dein Problem damit nicht.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2021869</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2021869</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Wed, 16 Feb 2011 15:21:45 GMT</pubDate></item></channel></rss>