<?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[Wie räume ich richtig hinter mir auf?]]></title><description><![CDATA[<p>Hallo Community!</p>
<p>Ich habe hier folgendes Minimalbeispiel:</p>
<pre><code class="language-cpp">Eingabe: s vom Typ int

test = new float**[s];
for (int x = 0; x &lt; s; x++)
{
   test[x] = new float*[s];
   for (int y = 0; y &lt; s; y++) neighborhood[x][y] = new float[s];
}
</code></pre>
<p>Wie kann ich diesen Speicher am Ende richtig aufräumen, sodass ich keine Speicherlöscher erzeuge?</p>
<p>Viele Grüße</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/305911/wie-räume-ich-richtig-hinter-mir-auf</link><generator>RSS for Node</generator><lastBuildDate>Sat, 08 Aug 2026 12:07:48 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/305911.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 12 Jul 2012 07:28:53 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Wie räume ich richtig hinter mir auf? on Thu, 12 Jul 2012 07:28:53 GMT]]></title><description><![CDATA[<p>Hallo Community!</p>
<p>Ich habe hier folgendes Minimalbeispiel:</p>
<pre><code class="language-cpp">Eingabe: s vom Typ int

test = new float**[s];
for (int x = 0; x &lt; s; x++)
{
   test[x] = new float*[s];
   for (int y = 0; y &lt; s; y++) neighborhood[x][y] = new float[s];
}
</code></pre>
<p>Wie kann ich diesen Speicher am Ende richtig aufräumen, sodass ich keine Speicherlöscher erzeuge?</p>
<p>Viele Grüße</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2232113</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2232113</guid><dc:creator><![CDATA[host]]></dc:creator><pubDate>Thu, 12 Jul 2012 07:28:53 GMT</pubDate></item><item><title><![CDATA[Reply to Wie räume ich richtig hinter mir auf? on Thu, 12 Jul 2012 07:29:48 GMT]]></title><description><![CDATA[<p>Indem Du std::vector und keine rohen Zeiger benutzt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2232114</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2232114</guid><dc:creator><![CDATA[Tachyon]]></dc:creator><pubDate>Thu, 12 Jul 2012 07:29:48 GMT</pubDate></item><item><title><![CDATA[Reply to Wie räume ich richtig hinter mir auf? on Thu, 12 Jul 2012 07:39:54 GMT]]></title><description><![CDATA[<p>Für jedes new[] wird ein delete[] fällig. Allerdings solltest du immer, wenn du new[] und delete[] einsetzen willst, dich fragen, ob es wirklich eine gute Idee ist, das zu tun; denn statt</p>
<pre><code class="language-cpp">void foo()
{
  int* ptr = new int[123];
  ...
  delete[] ptr;
}
</code></pre>
<p>ist folgendes einfacher und weniger fehleranfällig:</p>
<pre><code class="language-cpp">void foo()
{
  std::vector&lt;int&gt; vec (123);
  ...
}
</code></pre>
<p>siehe<br />
<a href="http://de.wikipedia.org/wiki/Ressourcenbelegung_ist_Initialisierung" rel="nofollow">http://de.wikipedia.org/wiki/Ressourcenbelegung_ist_Initialisierung</a></p>
<p>Viele solcher Klassen gibt es ja schon in der Standardbibliothek (stream-Klassen , STL-Container, etc etc etc). Und falls du eine solche selbst bauen willst, solltest du noch<br />
<a href="http://de.wikipedia.org/wiki/Dreierregel_(C%2B%2B)" rel="nofollow">http://de.wikipedia.org/wiki/Dreierregel_(C%2B%2B)</a><br />
gelesen haben.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2232120</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2232120</guid><dc:creator><![CDATA[krümelkacker]]></dc:creator><pubDate>Thu, 12 Jul 2012 07:39:54 GMT</pubDate></item><item><title><![CDATA[Reply to Wie räume ich richtig hinter mir auf? on Sun, 15 Jul 2012 12:08:17 GMT]]></title><description><![CDATA[<p>Danke für die Antworten. Das hab ich befürchtet. Dadurch wird der Zugriff aufwändiger.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2232929</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2232929</guid><dc:creator><![CDATA[host]]></dc:creator><pubDate>Sun, 15 Jul 2012 12:08:17 GMT</pubDate></item><item><title><![CDATA[Reply to Wie räume ich richtig hinter mir auf? on Sun, 15 Jul 2012 12:10:33 GMT]]></title><description><![CDATA[<p>host schrieb:</p>
<blockquote>
<p>Dadurch wird der Zugriff aufwändiger.</p>
</blockquote>
<p>Wieso?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2232931</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2232931</guid><dc:creator><![CDATA[icarus2]]></dc:creator><pubDate>Sun, 15 Jul 2012 12:10:33 GMT</pubDate></item><item><title><![CDATA[Reply to Wie räume ich richtig hinter mir auf? on Sun, 15 Jul 2012 14:38:14 GMT]]></title><description><![CDATA[<p>Da es hier um eine Art Würfel geht, werf ich mal <a href="http://www.boost.org/doc/libs/1_50_0/libs/multi_array/doc/index.html" rel="nofollow">Boost.Multi-Array</a> in die Diskussion.</p>
<pre><code class="language-cpp">test = new float**[s];
for (int x = 0; x &lt; s; x++)
{
   test[x] = new float*[s];
   for (int y = 0; y &lt; s; y++) neighborhood[x][y] = new float[s];
}
</code></pre>
<p>So jedenfalls sollte man das schon deshalb nicht machen, weil new std::bad_alloc werfen kann und man ggf. hinterher nicht mehr weiß, bis wohin man schon Zeiger auf Speicher hat, der wieder freigegeben werden muss. Man könnte dieses spezielle Problem zwar mit</p>
<pre><code class="language-cpp">test[x] = new(std::nothrow) float*[s];
if(test[x]) {
  for(int y = 0; y &lt; s; ++y) {
    neighborhood[x][y] = new(std::nothrow) float[s];
  }
}
</code></pre>
<p>o.ä. umschiffen, aber die Fehlerbehandlung wäre immer noch ausgesprochen widerlich. Dagegen ist</p>
<pre><code class="language-cpp">#include &lt;boost/multi_array.hpp&gt;

...

boost::multi_array&lt;float, 3&gt; test(boost::extents[s][s][s]);
</code></pre>
<p>gleich vernünftig verRAIIt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2232956</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2232956</guid><dc:creator><![CDATA[seldon]]></dc:creator><pubDate>Sun, 15 Jul 2012 14:38:14 GMT</pubDate></item></channel></rss>