<?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[Probleme mit delete]]></title><description><![CDATA[<p>Hallo zusammen,</p>
<p>folgender Codeschnippsel:</p>
<pre><code>// Get all icon objects from the vector and write its names into the XML file
	for (int i=1; i&lt; numberOfIcons; i++)
	{
		string temp = myIcons.getIconVector().at(i)-&gt;getName();
		char *buf = new char[strlen(temp.c_str())];

		strcpy(buf, temp.c_str());

		XML.write(buf);

		//...When you are done with buf...
		delete []buf;

	}
</code></pre>
<p>Verursacht eine Heap-Corruption an der <strong>delete []buf;</strong></p>
<p>Hat jemand eine Idee, warum?</p>
<p>gruß<br />
Alex</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/226227/probleme-mit-delete</link><generator>RSS for Node</generator><lastBuildDate>Thu, 01 Oct 2026 01:17:58 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/226227.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 30 Oct 2008 19:46:42 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Probleme mit delete on Thu, 30 Oct 2008 19:46:42 GMT]]></title><description><![CDATA[<p>Hallo zusammen,</p>
<p>folgender Codeschnippsel:</p>
<pre><code>// Get all icon objects from the vector and write its names into the XML file
	for (int i=1; i&lt; numberOfIcons; i++)
	{
		string temp = myIcons.getIconVector().at(i)-&gt;getName();
		char *buf = new char[strlen(temp.c_str())];

		strcpy(buf, temp.c_str());

		XML.write(buf);

		//...When you are done with buf...
		delete []buf;

	}
</code></pre>
<p>Verursacht eine Heap-Corruption an der <strong>delete []buf;</strong></p>
<p>Hat jemand eine Idee, warum?</p>
<p>gruß<br />
Alex</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1607442</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1607442</guid><dc:creator><![CDATA[filth]]></dc:creator><pubDate>Thu, 30 Oct 2008 19:46:42 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme mit delete on Thu, 30 Oct 2008 19:49:09 GMT]]></title><description><![CDATA[<p>Ich glaub zwar nicht, dass es daran liegt, aber du solltest mit <code>new[]</code> ein Byte mehr Speicher anfordern, da <code>strlen()</code> nur die Länge des C-Strings <strong>ohne</strong> Nullterminierung zurückgibt.</p>
<p>Ansonsten, deletest du zwei Mal, oder führst du sonstige Speicherzugriffe durch?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1607443</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1607443</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Thu, 30 Oct 2008 19:49:09 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme mit delete on Thu, 30 Oct 2008 19:52:32 GMT]]></title><description><![CDATA[<p>Danke!</p>
<p>Anscheinend lag es doch daran, mit :</p>
<p>char *buf = new char[strlen(temp.c_str())+1];</p>
<p>funktioniert es. Aber warum verstehe ich nicht <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1607444</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1607444</guid><dc:creator><![CDATA[filth]]></dc:creator><pubDate>Thu, 30 Oct 2008 19:52:32 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme mit delete on Thu, 30 Oct 2008 19:56:19 GMT]]></title><description><![CDATA[<p>filth schrieb:</p>
<blockquote>
<p>Aber warum verstehe ich nicht <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /></p>
</blockquote>
<p>Steht aber in meinem oberen Post. Du solltest schon genug Speicher anfordern, sonst wird das <code>'\0'</code> irgendwo hingeschrieben.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1607448</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1607448</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Thu, 30 Oct 2008 19:56:19 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme mit delete on Thu, 30 Oct 2008 19:57:39 GMT]]></title><description><![CDATA[<p>Achso und delete versucht dann quasi einen zu langen Speicherabschnitt zu löschen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1607449</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1607449</guid><dc:creator><![CDATA[filth]]></dc:creator><pubDate>Thu, 30 Oct 2008 19:57:39 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme mit delete on Thu, 30 Oct 2008 19:58:07 GMT]]></title><description><![CDATA[<p>weil das nullbyte am ende eines c-strings auch platz braucht und strlen gibt die länge des strings ohne dieses byte zurück. du willst ja die länge wissen, nicht die größe des benötigten speichers.</p>
<p>wenn diese string-variable ein std::string ist, dann wäre folgender code besser:</p>
<pre><code class="language-cpp">// Get all icon objects from the vector and write its names into the XML file
	for (int i=1; i&lt; numberOfIcons; i++)
	{
            string temp = myIcons.getIconVector().at(i)-&gt;getName();
            XML.write(temp.c_str());
	}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1607450</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1607450</guid><dc:creator><![CDATA[pony reiten!]]></dc:creator><pubDate>Thu, 30 Oct 2008 19:58:07 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme mit delete on Thu, 30 Oct 2008 20:02:31 GMT]]></title><description><![CDATA[<p>filth schrieb:</p>
<blockquote>
<p>Achso und delete versucht dann quasi einen zu langen Speicherabschnitt zu löschen?</p>
</blockquote>
<p>Nein, das <code>delete[]</code> löscht so viele Bytes, wie mit <code>new[]</code> angefordert wurden (vorausgesetzt, du hast einen gültigen Zeiger).</p>
<p><code>strcpy()</code> ist das Problem, da es alle <code>char</code> -Elemente inklusive <code>'\0'</code> kopiert und nicht darauf achtet, ob genügend Speicher vorhanden ist oder nicht.</p>
<p>Das ist übrigens eines der Probleme von C, in C++ kann man das mit dem Datentyp <code>std::string</code> umgehen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1607452</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1607452</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Thu, 30 Oct 2008 20:02:31 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme mit delete on Fri, 31 Oct 2008 06:03:09 GMT]]></title><description><![CDATA[<p>Der Vollständigkeit halber:<br />
1. strlen brauchst Du nicht zu bemühen, das kostet nur Zeit. string hat eine Methode size<br />
2. Wenn Du wirklich einen non-const Puffer brauchst, nimm C++-Mittel:</p>
<pre><code class="language-cpp">{
        string temp = myIcons.getIconVector().at(i)-&gt;getName();
        vector&lt;char&gt; buf(temp.c_str(), temp.c_str() + temp.size() + 1);

        XML.write(&amp;buf[0]);

        //...When you are done with buf...
        // ... NOTHING, 'cause this is C++
    }
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1607775</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1607775</guid><dc:creator><![CDATA[LordJaxom]]></dc:creator><pubDate>Fri, 31 Oct 2008 06:03:09 GMT</pubDate></item></channel></rss>