<?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[Eigene memcpy Implementierung crasht]]></title><description><![CDATA[<p>Hi, warum crasht denn bei folgender memcpy implementierung das Programm bzw. es kommt immer die Meldung, dass der Stack um items currupiert worden wäre.</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;

void Copy(void* sourceArray, void* destinationArray, int count)
{
	while (count--)
	{
		*(char*)destinationArray = *(char*)sourceArray;
		destinationArray = (char*)destinationArray + 1;
		sourceArray = (char*)sourceArray + 1;
	}
}

int _tmain(int argc, _TCHAR* argv[])
{
	int* items = new int[10];
	int* newitems = new int[10];

	items[0] = 1;
	items[1] = 2;
	items[2] = 3;
	items[3] = 4;
	items[4] = 5;
	items[5] = 6;
	items[6] = 7;
	items[7] = 8;
	items[8] = 9;
	items[9] = 10;

	Copy(&amp;items, &amp;newitems, 10);

	for (int i = 0; i &lt; 10; ++i)
	{
		std::wcout &lt;&lt; newitems[i] &lt;&lt; std::endl;
	}

	std::wcout &lt;&lt; L&quot;Press any key to continue.&quot; &lt;&lt; std::endl;
	std::wcin.get();
	delete[] items;
	delete[] newitems;
	return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/309370/eigene-memcpy-implementierung-crasht</link><generator>RSS for Node</generator><lastBuildDate>Wed, 05 Aug 2026 06:30:43 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/309370.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 21 Oct 2012 19:39:01 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Eigene memcpy Implementierung crasht on Sun, 21 Oct 2012 19:39:01 GMT]]></title><description><![CDATA[<p>Hi, warum crasht denn bei folgender memcpy implementierung das Programm bzw. es kommt immer die Meldung, dass der Stack um items currupiert worden wäre.</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;

void Copy(void* sourceArray, void* destinationArray, int count)
{
	while (count--)
	{
		*(char*)destinationArray = *(char*)sourceArray;
		destinationArray = (char*)destinationArray + 1;
		sourceArray = (char*)sourceArray + 1;
	}
}

int _tmain(int argc, _TCHAR* argv[])
{
	int* items = new int[10];
	int* newitems = new int[10];

	items[0] = 1;
	items[1] = 2;
	items[2] = 3;
	items[3] = 4;
	items[4] = 5;
	items[5] = 6;
	items[6] = 7;
	items[7] = 8;
	items[8] = 9;
	items[9] = 10;

	Copy(&amp;items, &amp;newitems, 10);

	for (int i = 0; i &lt; 10; ++i)
	{
		std::wcout &lt;&lt; newitems[i] &lt;&lt; std::endl;
	}

	std::wcout &lt;&lt; L&quot;Press any key to continue.&quot; &lt;&lt; std::endl;
	std::wcin.get();
	delete[] items;
	delete[] newitems;
	return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2262582</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2262582</guid><dc:creator><![CDATA[Enumerator]]></dc:creator><pubDate>Sun, 21 Oct 2012 19:39:01 GMT</pubDate></item><item><title><![CDATA[Reply to Eigene memcpy Implementierung crasht on Sun, 21 Oct 2012 19:53:38 GMT]]></title><description><![CDATA[<p>Wieso zum Teufel so viele Pointer und Freispeicherallokierung? Kein Wunder <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /> das ist C mit <code>cin</code> und <code>cout</code> (und <code>new</code> und <code>delete</code> ).</p>
<p>Sieh dir mal dynamische/statische Arrays in C++ an. Da nutzt man die STL-Container. Dazu gibt es passendes Zubehör (STL-Algorithmen). Dann steht schon kein <code>new</code> / <code>delete</code> mehr in deinem Programm. Und dann wirst du auch nie wieder <code>memcpy</code> benutzen.</p>
<p>P.S.: Die casts die du da durchführst sind auch fragwürdig. Ich frag mich, ob das UB ist....</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2262588</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2262588</guid><dc:creator><![CDATA[Sone]]></dc:creator><pubDate>Sun, 21 Oct 2012 19:53:38 GMT</pubDate></item><item><title><![CDATA[Reply to Eigene memcpy Implementierung crasht on Sun, 21 Oct 2012 20:11:54 GMT]]></title><description><![CDATA[<p>Zeile 29: Du gibst der Funktion die Adresse der Zeiger. Höchstwahrscheinlich nicht das was du möchtest.</p>
<p>Beim Querlesen habe ich noch vieles anderes verdächtiges und gefährliches gesehen, aber bevor ich ins Detail gehe: Wozu soll das überhaupt gut sein?</p>
<p>Dein Copy an sich sollte übrigens richtig sein, auch wenn man das sehr leicht wesentlich schöner machen könnte, zum Beispiel durch const-correctness und hübschere Schreibweise:</p>
<pre><code class="language-cpp">void my_memcpy(void* dest, const void* src, size_t count) 
{
 char* char_dest = (char*)dest;  // Oder C++-Casts, damit Sone keinen Anfall bekommt
 char* char_src = (char*)src;

 while (count--)
  *char_dst++ = *char_src++;  
}
</code></pre>
<p>Macht genau das gleiche wie deine Variante.</p>
<p>edit:</p>
<p>Sone schrieb:</p>
<blockquote>
<p>P.S.: Die casts die du da durchführst sind auch fragwürdig. Ich frag mich, ob das UB ist....</p>
</blockquote>
<p>Die sollten in Ordnung sein. Was stört dich an denen? Oder ist das einfach dein anerzogener Beißreflex, wenn du einen C-Cast siehst?</p>
<p>edit2: Schreibfehler im Code korrigiert.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2262589</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2262589</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Sun, 21 Oct 2012 20:11:54 GMT</pubDate></item><item><title><![CDATA[Reply to Eigene memcpy Implementierung crasht on Sun, 21 Oct 2012 20:01:50 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">Copy(items, newitems, 10 * sizeof(int));
// bzw.
Copy(items, newitems, 10 * sizeof(*items));
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2262590</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2262590</guid><dc:creator><![CDATA[ghjghjghj]]></dc:creator><pubDate>Sun, 21 Oct 2012 20:01:50 GMT</pubDate></item><item><title><![CDATA[Reply to Eigene memcpy Implementierung crasht on Sun, 21 Oct 2012 20:08:31 GMT]]></title><description><![CDATA[<p>Und kürzer gehts auch noch (schöner ist es dadurch aber nicht):</p>
<pre><code class="language-cpp">for (char* char_dst = (char*)dst, *char_src = (char*)src; count--;) 
 *char_dst++ = *char_src++;
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2262596</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2262596</guid><dc:creator><![CDATA[uiuzizui]]></dc:creator><pubDate>Sun, 21 Oct 2012 20:08:31 GMT</pubDate></item><item><title><![CDATA[Reply to Eigene memcpy Implementierung crasht on Sun, 21 Oct 2012 20:09:43 GMT]]></title><description><![CDATA[<p>Du hast ein * vergessen im Code SeppJ.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2262597</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2262597</guid><dc:creator><![CDATA[cooky451]]></dc:creator><pubDate>Sun, 21 Oct 2012 20:09:43 GMT</pubDate></item><item><title><![CDATA[Reply to Eigene memcpy Implementierung crasht on Sun, 21 Oct 2012 20:11:20 GMT]]></title><description><![CDATA[<p>cooky451 schrieb:</p>
<blockquote>
<p>Du hast ein * vergessen im Code SeppJ.</p>
</blockquote>
<p>Ups! Danke! Das kommt davon, wenn man im Editfenster programmiert.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2262600</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2262600</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Sun, 21 Oct 2012 20:11:20 GMT</pubDate></item><item><title><![CDATA[Reply to Eigene memcpy Implementierung crasht on Sun, 21 Oct 2012 20:41:12 GMT]]></title><description><![CDATA[<p>SeppJ schrieb:</p>
<blockquote>
<p>Sone schrieb:</p>
<blockquote>
<p>P.S.: Die casts die du da durchführst sind auch fragwürdig. Ich frag mich, ob das UB ist....</p>
</blockquote>
<p>Die sollten in Ordnung sein. Was stört dich an denen? Oder ist das einfach dein anerzogener Beißreflex, wenn du einen C-Cast siehst?</p>
</blockquote>
<p>Nein, nein. Ich meinte dieser Cast von <code>int*</code> nach <code>void*</code> und dann nach <code>char*</code> - ist das so vollkommen in Ordnung?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2262606</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2262606</guid><dc:creator><![CDATA[Sone]]></dc:creator><pubDate>Sun, 21 Oct 2012 20:41:12 GMT</pubDate></item><item><title><![CDATA[Reply to Eigene memcpy Implementierung crasht on Mon, 22 Oct 2012 06:17:17 GMT]]></title><description><![CDATA[<p>Sone schrieb:</p>
<blockquote>
<p>SeppJ schrieb:</p>
<blockquote>
<p>Sone schrieb:</p>
<blockquote>
<p>P.S.: Die casts die du da durchführst sind auch fragwürdig. Ich frag mich, ob das UB ist....</p>
</blockquote>
<p>Die sollten in Ordnung sein. Was stört dich an denen? Oder ist das einfach dein anerzogener Beißreflex, wenn du einen C-Cast siehst?</p>
</blockquote>
<p>Nein, nein. Ich meinte dieser Cast von <code>int*</code> nach <code>void*</code> und dann nach <code>char*</code> - ist das so vollkommen in Ordnung?</p>
</blockquote>
<p>Warum sollte es das nicht sein?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2262657</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2262657</guid><dc:creator><![CDATA[Shade Of Mine]]></dc:creator><pubDate>Mon, 22 Oct 2012 06:17:17 GMT</pubDate></item><item><title><![CDATA[Reply to Eigene memcpy Implementierung crasht on Mon, 22 Oct 2012 06:17:26 GMT]]></title><description><![CDATA[<p>Sone schrieb:</p>
<blockquote>
<p>ist das so vollkommen in Ordnung?</p>
</blockquote>
<p>Ja. Nach void* und char* darf immer gecastet werden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2262658</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2262658</guid><dc:creator><![CDATA[pumuckl]]></dc:creator><pubDate>Mon, 22 Oct 2012 06:17:26 GMT</pubDate></item><item><title><![CDATA[Reply to Eigene memcpy Implementierung crasht on Mon, 22 Oct 2012 06:18:22 GMT]]></title><description><![CDATA[<p>Keine Ahnung. Ich habe einen Beißreflex auf Pointer-Casts. Das sieht mir einfach immer so eklig aus <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2262659</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2262659</guid><dc:creator><![CDATA[Sone]]></dc:creator><pubDate>Mon, 22 Oct 2012 06:18:22 GMT</pubDate></item><item><title><![CDATA[Reply to Eigene memcpy Implementierung crasht on Mon, 22 Oct 2012 11:28:18 GMT]]></title><description><![CDATA[<p>Anders wäre memcpy sowieso nicht vernünftig implementierbar.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2262821</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2262821</guid><dc:creator><![CDATA[Ethon]]></dc:creator><pubDate>Mon, 22 Oct 2012 11:28:18 GMT</pubDate></item><item><title><![CDATA[Reply to Eigene memcpy Implementierung crasht on Mon, 22 Oct 2012 14:48:10 GMT]]></title><description><![CDATA[<p>Sone schrieb:</p>
<blockquote>
<p>Keine Ahnung. Ich habe einen Beißreflex auf Pointer-Casts. Das sieht mir einfach immer so eklig aus <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /></p>
</blockquote>
<p>Womit wir wieder beim Thema wären: einfach mal die Fresse halten wenn man nix zu sagen hat.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2262899</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2262899</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Mon, 22 Oct 2012 14:48:10 GMT</pubDate></item><item><title><![CDATA[Reply to Eigene memcpy Implementierung crasht on Mon, 22 Oct 2012 15:05:51 GMT]]></title><description><![CDATA[<p>hustbaer schrieb:</p>
<blockquote>
<p>Sone schrieb:</p>
<blockquote>
<p>Keine Ahnung. Ich habe einen Beißreflex auf Pointer-Casts. Das sieht mir einfach immer so eklig aus <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /></p>
</blockquote>
<p>Womit wir wieder beim Thema wären: einfach mal die Fresse halten wenn man nix zu sagen hat.</p>
</blockquote>
<p>Ähm... und was hat das mit mir zu tun? Ich hab' die Frage gestellt, ob das ein gültiger Cast ist. Ich habe die Antwort bekommen. Und du übertreibst wieder in alle Maßen? Was los bei dir?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2262907</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2262907</guid><dc:creator><![CDATA[Sone]]></dc:creator><pubDate>Mon, 22 Oct 2012 15:05:51 GMT</pubDate></item><item><title><![CDATA[Reply to Eigene memcpy Implementierung crasht on Mon, 22 Oct 2012 20:00:12 GMT]]></title><description><![CDATA[<p>Ab SSE2 empfehle ich folgendes.</p>
<pre><code class="language-cpp">void nontemporal_copy(char* outbuff, char* inbuff, int size)
{
const int step = 64; // cache line
while(size &gt; step) {
  _mm_prefetch(inbuff + 320, _MM_HINT_NTA); // non-temporal prefetch
  __m128i A = _mm_loadu_si128((__m128i*) (inbuff +  0));
  __m128i B = _mm_loadu_si128((__m128i*) (inbuff + 16));
  __m128i C = _mm_loadu_si128((__m128i*) (inbuff + 32));
  __m128i D = _mm_loadu_si128((__m128i*) (inbuff + 48));

  // destination address must be 16-byte aligned!
  _mm_stream_si128((__m128i*) (outbuff +  0), A);
  _mm_stream_si128((__m128i*) (outbuff + 16), B);
  _mm_stream_si128((__m128i*) (outbuff + 32), C);
  _mm_stream_si128((__m128i*) (outbuff + 48), D);

  inbuff  += step;
  outbuff += step;
  size -= step;
}
_mm_mfence();
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2262999</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2262999</guid><dc:creator><![CDATA[nurf]]></dc:creator><pubDate>Mon, 22 Oct 2012 20:00:12 GMT</pubDate></item></channel></rss>