<?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[&amp;quot;delete&amp;quot; Frage]]></title><description><![CDATA[<p>Gehen wir mal davon aus wir haben folgenden Fall vorliegen:</p>
<pre><code class="language-cpp">class EineKlasse
{
}

class Test
{
   private:
       std::vector&lt;EineKlasse*&gt; zeigers;

  public:
    void einefunktion(int wert)
    {
        for(int i =0;i&lt;wert;i++)
        {
            zeigers.push_back(new EineKlasse);
        }
    }
  ~Test()
   {
       //muss ich hier wieder jedes element im vector abbräumen? ála
       for(int i =0;i&lt;zeigers.size();i++)
       {
           delete zeigers[i];
       }
    }

};
</code></pre>
<p>Muss ich wie oben beschrieben, die Elemente nach dem &quot;Gesetzt: Für jedes new ein delete&quot; auch so wieder abräumen? Oder übernimmt das der Vector für mich?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/231292/quot-delete-quot-frage</link><generator>RSS for Node</generator><lastBuildDate>Thu, 24 Sep 2026 07:10:38 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/231292.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 08 Jan 2009 13:35:22 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to &amp;quot;delete&amp;quot; Frage on Thu, 08 Jan 2009 13:35:22 GMT]]></title><description><![CDATA[<p>Gehen wir mal davon aus wir haben folgenden Fall vorliegen:</p>
<pre><code class="language-cpp">class EineKlasse
{
}

class Test
{
   private:
       std::vector&lt;EineKlasse*&gt; zeigers;

  public:
    void einefunktion(int wert)
    {
        for(int i =0;i&lt;wert;i++)
        {
            zeigers.push_back(new EineKlasse);
        }
    }
  ~Test()
   {
       //muss ich hier wieder jedes element im vector abbräumen? ála
       for(int i =0;i&lt;zeigers.size();i++)
       {
           delete zeigers[i];
       }
    }

};
</code></pre>
<p>Muss ich wie oben beschrieben, die Elemente nach dem &quot;Gesetzt: Für jedes new ein delete&quot; auch so wieder abräumen? Oder übernimmt das der Vector für mich?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1641740</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1641740</guid><dc:creator><![CDATA[Firefighter]]></dc:creator><pubDate>Thu, 08 Jan 2009 13:35:22 GMT</pubDate></item><item><title><![CDATA[Reply to &amp;quot;delete&amp;quot; Frage on Thu, 08 Jan 2009 13:37:35 GMT]]></title><description><![CDATA[<p>Firefighter schrieb:</p>
<blockquote>
<p>...<br />
Muss ich wie oben beschrieben, die Elemente nach dem &quot;Gesetzt: Für jedes new ein delete&quot; auch so wieder abräumen? ...</p>
</blockquote>
<p>Ja.</p>
<p>Firefighter schrieb:</p>
<blockquote>
<p>...<br />
Oder übernimmt das der Vector für mich?</p>
</blockquote>
<p>Nein.<br />
Der vector destruiert nur die Objekt, die sich in ihm befinden ... in diesem Fall <em>Zeiger</em>.</p>
<p>Gruß,</p>
<p>Simon2.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1641743</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1641743</guid><dc:creator><![CDATA[Simon2]]></dc:creator><pubDate>Thu, 08 Jan 2009 13:37:35 GMT</pubDate></item><item><title><![CDATA[Reply to &amp;quot;delete&amp;quot; Frage on Thu, 08 Jan 2009 13:37:46 GMT]]></title><description><![CDATA[<p>Der Vector löscht nur die Pointer selbst, nicht die Objekte auf die sie zeigen. Musst du selbst übernehmen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1641744</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1641744</guid><dc:creator><![CDATA[Fellhuhn]]></dc:creator><pubDate>Thu, 08 Jan 2009 13:37:46 GMT</pubDate></item><item><title><![CDATA[Reply to &amp;quot;delete&amp;quot; Frage on Thu, 08 Jan 2009 13:38:10 GMT]]></title><description><![CDATA[<p>Ok alles klar, und der Dtor hat auch so seine richtigkeit, ja?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1641745</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1641745</guid><dc:creator><![CDATA[Firefighter]]></dc:creator><pubDate>Thu, 08 Jan 2009 13:38:10 GMT</pubDate></item><item><title><![CDATA[Reply to &amp;quot;delete&amp;quot; Frage on Thu, 08 Jan 2009 13:39:30 GMT]]></title><description><![CDATA[<p>Firefighter schrieb:</p>
<blockquote>
<p>Ok alles klar, und der Dtor hat auch so seine richtigkeit, ja?</p>
</blockquote>
<p>Ja!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1641746</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1641746</guid><dc:creator><![CDATA[David_pb]]></dc:creator><pubDate>Thu, 08 Jan 2009 13:39:30 GMT</pubDate></item><item><title><![CDATA[Reply to &amp;quot;delete&amp;quot; Frage on Thu, 08 Jan 2009 13:40:01 GMT]]></title><description><![CDATA[<p>Ok danke, hab ichs mir doch richtig vorgestellt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1641747</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1641747</guid><dc:creator><![CDATA[Firefighter]]></dc:creator><pubDate>Thu, 08 Jan 2009 13:40:01 GMT</pubDate></item><item><title><![CDATA[Reply to &amp;quot;delete&amp;quot; Frage on Thu, 08 Jan 2009 13:44:38 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">std::vector&lt;std::auto_ptr&lt;Kontobewegung&gt; &gt; bewegungen;
</code></pre>
<p>Wenn ich das so gestalte(deklaration scheint noch fehlerhaft zu sein, da ich fehler bekomme) brauche ich mich später nicht über das aufräumem kümmern richtig`?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1641753</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1641753</guid><dc:creator><![CDATA[Firefighter]]></dc:creator><pubDate>Thu, 08 Jan 2009 13:44:38 GMT</pubDate></item><item><title><![CDATA[Reply to &amp;quot;delete&amp;quot; Frage on Thu, 08 Jan 2009 13:48:13 GMT]]></title><description><![CDATA[<p>Theoretisch hast du da recht!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1641756</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1641756</guid><dc:creator><![CDATA[David_pb]]></dc:creator><pubDate>Thu, 08 Jan 2009 13:48:13 GMT</pubDate></item><item><title><![CDATA[Reply to &amp;quot;delete&amp;quot; Frage on Thu, 08 Jan 2009 13:48:47 GMT]]></title><description><![CDATA[<p>Firefighter schrieb:</p>
<blockquote>
<p>Wenn ich das so gestalte(deklaration scheint noch fehlerhaft zu sein, da ich fehler bekomme) brauche ich mich später nicht über das aufräumem kümmern richtig`?</p>
</blockquote>
<p>Falsch. auto_ptr ist wegen seiner Kopiersemantik nicht für die Container der Standardbibliothek geeignet.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1641758</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1641758</guid><dc:creator><![CDATA[MFK]]></dc:creator><pubDate>Thu, 08 Jan 2009 13:48:47 GMT</pubDate></item><item><title><![CDATA[Reply to &amp;quot;delete&amp;quot; Frage on Thu, 08 Jan 2009 13:49:40 GMT]]></title><description><![CDATA[<p>EDIT: Ok MFK ich danke dir, das erklärt einige Fehler.</p>
<p>Mit den shared_ptr, hab ich recht? aber mal ne andere Frage, wenn meine deklaration wie oben aussieht, wie könnte dann ein pushback eines möglichen kontobewegung-zeiger aussehen?Die Referenz ist mir dabei gerade nicht sehr hilfreich <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="😃"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1641759</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1641759</guid><dc:creator><![CDATA[Firefighter]]></dc:creator><pubDate>Thu, 08 Jan 2009 13:49:40 GMT</pubDate></item><item><title><![CDATA[Reply to &amp;quot;delete&amp;quot; Frage on Thu, 08 Jan 2009 13:51:48 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">Kontenbewegung *bestechung = new Kontenbewegung();
bewegungen.push_back(bestechung);
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1641763</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1641763</guid><dc:creator><![CDATA[Fellhuhn]]></dc:creator><pubDate>Thu, 08 Jan 2009 13:51:48 GMT</pubDate></item><item><title><![CDATA[Reply to &amp;quot;delete&amp;quot; Frage on Thu, 08 Jan 2009 13:53:05 GMT]]></title><description><![CDATA[<p>Öhmm...nee ich glaube das meinte ich nicht...sondern ich meinte eher, wie es mit shared_ptr ausehen müsste?Weil mit den burschen hab ich noch net so viel erfahrung.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1641766</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1641766</guid><dc:creator><![CDATA[Firefighter]]></dc:creator><pubDate>Thu, 08 Jan 2009 13:53:05 GMT</pubDate></item><item><title><![CDATA[Reply to &amp;quot;delete&amp;quot; Frage on Thu, 08 Jan 2009 13:54:28 GMT]]></title><description><![CDATA[<p>So?</p>
<pre><code class="language-cpp">bewegungen.push_back( shared_ptr&lt;Kontenbewegung&gt;( new Kontenbewegung() ) );
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1641769</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1641769</guid><dc:creator><![CDATA[David_pb]]></dc:creator><pubDate>Thu, 08 Jan 2009 13:54:28 GMT</pubDate></item><item><title><![CDATA[Reply to &amp;quot;delete&amp;quot; Frage on Thu, 08 Jan 2009 13:56:08 GMT]]></title><description><![CDATA[<p>Ok das hilft mir schonmal weiter, und die Deklaration vom vector wäre dann so hier?:</p>
<pre><code class="language-cpp">std::vector&lt; shared_ptr&lt;Irgendwas&gt; &gt; vieles_irgendwas;
</code></pre>
<p>?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1641771</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1641771</guid><dc:creator><![CDATA[Firefighter]]></dc:creator><pubDate>Thu, 08 Jan 2009 13:56:08 GMT</pubDate></item><item><title><![CDATA[Reply to &amp;quot;delete&amp;quot; Frage on Thu, 08 Jan 2009 13:57:03 GMT]]></title><description><![CDATA[<p>Sieht ok aus.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1641773</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1641773</guid><dc:creator><![CDATA[David_pb]]></dc:creator><pubDate>Thu, 08 Jan 2009 13:57:03 GMT</pubDate></item><item><title><![CDATA[Reply to &amp;quot;delete&amp;quot; Frage on Thu, 08 Jan 2009 13:58:00 GMT]]></title><description><![CDATA[<p>Und auf die deklaration kann ich dein push_back anwenden? Kann doch eigentlich bei deinem push_back das führende shared&lt;&gt; zeugs weglassen und nur ein normales new machen oder nicht?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1641775</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1641775</guid><dc:creator><![CDATA[Firefighter]]></dc:creator><pubDate>Thu, 08 Jan 2009 13:58:00 GMT</pubDate></item><item><title><![CDATA[Reply to &amp;quot;delete&amp;quot; Frage on Thu, 08 Jan 2009 14:04:47 GMT]]></title><description><![CDATA[<p>OK hat sich erledigt, habs alleine hinbekommen. Aber mal ne andere Frage, worin besteht nun der Underschied zwischen auto_ptr und shared_ptr?also aufjedenfall schonmal die Containerkompatiblität, und das shared_ptr sich ums aufräumen kümmert, aber was noch?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1641781</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1641781</guid><dc:creator><![CDATA[Firefighter]]></dc:creator><pubDate>Thu, 08 Jan 2009 14:04:47 GMT</pubDate></item><item><title><![CDATA[Reply to &amp;quot;delete&amp;quot; Frage on Thu, 08 Jan 2009 14:15:11 GMT]]></title><description><![CDATA[<p>Ok hab mir auch das ebend selbst beantwortet, danke für eure Beiträge.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1641785</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1641785</guid><dc:creator><![CDATA[Firefighter]]></dc:creator><pubDate>Thu, 08 Jan 2009 14:15:11 GMT</pubDate></item><item><title><![CDATA[Reply to &amp;quot;delete&amp;quot; Frage on Thu, 08 Jan 2009 17:04:27 GMT]]></title><description><![CDATA[<p>Vielleicht kannst du dir auch mal <em>Boost.Pointer Container</em> anschauen, da werden die Zeiger auch automatisch freigegeben.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1641941</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1641941</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Thu, 08 Jan 2009 17:04:27 GMT</pubDate></item><item><title><![CDATA[Reply to &amp;quot;delete&amp;quot; Frage on Thu, 08 Jan 2009 17:07:28 GMT]]></title><description><![CDATA[<p>Alles klar Nexus, danke für den Hinweis, kannste eventuell kurz erklären wo der signifikanteste Unterschied liegt?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1641944</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1641944</guid><dc:creator><![CDATA[Firefighter]]></dc:creator><pubDate>Thu, 08 Jan 2009 17:07:28 GMT</pubDate></item><item><title><![CDATA[Reply to &amp;quot;delete&amp;quot; Frage on Thu, 08 Jan 2009 17:21:00 GMT]]></title><description><![CDATA[<p>Die Pointer-Container speichern intern gerade Zeiger (wie könnte es anders sein ;)). Es existieren die analogen Container zur STL, wie <code>ptr_vector</code> , <code>ptr_deque</code> , <code>ptr_list</code> etc.</p>
<p>Das führt zu verschiedenen Vorteilen für den Anwender:</p>
<ul>
<li>Die gespeicherten Elemente müssen im Gegensatz zu STL-Containern nicht kopierkonstruierbar oder zuweisbar sein.</li>
<li>Bei Array-Containern wie <code>boost::ptr_vector</code> sind interne Operationen wie Löschungen und Einfügungen performanter, da nur Zeiger umgehängt werden.</li>
<li>Die Indirektion entfällt beim Aufruf. Man kann direkt schreiben <code>vec[1]</code> oder <code>vec.front()</code> , anstatt dass man noch einmal dereferenzieren muss.</li>
</ul>
<p>Siehe auch <a href="http://www.boost.org/doc/libs/1_37_0/libs/ptr_container/doc/ptr_container.html" rel="nofollow">hier</a>.</p>
<p>P.S. Die ersten beiden Punkte beziehen sich auf den Vergleich zu STL-Containern mit Objekten, der dritte zu STL-Containern mit Smart-Pointern.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1641948</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1641948</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Thu, 08 Jan 2009 17:21:00 GMT</pubDate></item><item><title><![CDATA[Reply to &amp;quot;delete&amp;quot; Frage on Thu, 08 Jan 2009 17:43:31 GMT]]></title><description><![CDATA[<p>Klasse, ich danke, das sind ja fabelhafte Geschichten.Haste super erklärt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1641964</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1641964</guid><dc:creator><![CDATA[Firefighter]]></dc:creator><pubDate>Thu, 08 Jan 2009 17:43:31 GMT</pubDate></item><item><title><![CDATA[Reply to &amp;quot;delete&amp;quot; Frage on Thu, 08 Jan 2009 22:41:03 GMT]]></title><description><![CDATA[<p>David_pb schrieb:</p>
<blockquote>
<p>So?</p>
<pre><code class="language-cpp">bewegungen.push_back( shared_ptr&lt;Kontenbewegung&gt;( new Kontenbewegung() ) );
</code></pre>
</blockquote>
<p>Das ist schlecht. Besser so:</p>
<pre><code class="language-cpp">shared_ptr&lt;Kontenbewegung&gt; kbw(new Kontenbewegung());
bewegungen.push_back(kbw);
</code></pre>
<p>Begründung bitte selbst in der Boost Doku nachlesen, danke.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1642137</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1642137</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Thu, 08 Jan 2009 22:41:03 GMT</pubDate></item><item><title><![CDATA[Reply to &amp;quot;delete&amp;quot; Frage on Fri, 09 Jan 2009 06:37:18 GMT]]></title><description><![CDATA[<p>Danke hustbaer, habs mir mal durchgelesen.Mir ist aber noch nicht ganz klar was das im Bezug auf mein Konstrukt ausmacht.Ich hab mir mal das Beispiel aus der Boost Doku durchgelesen:</p>
<pre><code class="language-cpp">void f(shared_ptr&lt;int&gt;, int);
int g();

void ok()
{
    shared_ptr&lt;int&gt; p(new int(2));
    f(p, g());
}

void bad()
{
    f(shared_ptr&lt;int&gt;(new int(2)), g());
}
</code></pre>
<p>Wenn ich das also richtig verstanden habe:</p>
<blockquote>
<p>Since function arguments are evaluated in unspecified order, it is possible for new int(2) to be evaluated first, g() second, and we may never get to the shared_ptr constructor if g throws an exception</p>
</blockquote>
<p>Das Funktionsargumente in keiner bestimmten Reihenfolge abgearbeitet werden können, und im Falle einer Exception wir niemals bei dem eigentlichen shared_ptr Konstrukt ankommen, ist das so richtig?Was bedeutet das aber in meinem Fall?</p>
<pre><code class="language-cpp">bewegungen.push_back( shared_ptr&lt;Kontenbewegung&gt;( new Kontenbewegung() ) );
</code></pre>
<p>Achso, es könnte also sein, das bei meiner erzeugung durch new eine Exception fliegt und ich dadurch nie bei shared_ptr ankomme?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1642212</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1642212</guid><dc:creator><![CDATA[Firefighter]]></dc:creator><pubDate>Fri, 09 Jan 2009 06:37:18 GMT</pubDate></item><item><title><![CDATA[Reply to &amp;quot;delete&amp;quot; Frage on Fri, 09 Jan 2009 06:49:47 GMT]]></title><description><![CDATA[<p>wenn beim new eine Exception fliegt kann das aus zwei Gründen geschehen:</p>
<ol>
<li>
<p>Fehler bei der Speicherallokation. Der Konstruktor des Objektes wird daher nie aufgerufen, alles paletti (außer dass du ohne Speicher schlecht dastehst)</p>
</li>
<li>
<p>Fehler im Ctor, dann werden die bereits konstruierten Teilobjekte wieder zerstört, die Exception fliegt und auch wieder kein Speicherleck - vorausgesetzt der Ctor ist exceptionsicher implementiert.</p>
</li>
</ol>
<p>Du kriegst durch ne Exception beim new also keine Speicherlecks<br />
Von der Exceptionsicherheit her kann man es also eigentlich als Einzeiler machen, wenns sich nur um einparametrige Funktionen handelt. Ob man sich das angewöhnen sollte und dann bei mehrparametrigen Funktionen immer dran denkt, es anders zu machen ist denke ich Geschmackssache. wirklich konsistent wäre es aber wohl nicht.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1642216</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1642216</guid><dc:creator><![CDATA[pumuckl]]></dc:creator><pubDate>Fri, 09 Jan 2009 06:49:47 GMT</pubDate></item><item><title><![CDATA[Reply to &amp;quot;delete&amp;quot; Frage on Fri, 09 Jan 2009 07:03:06 GMT]]></title><description><![CDATA[<p>Ok, was heißt das aber nun bei mir? Warum hat hustbaer eine andere Variante empfohlen, die wie sie dem Boost Standard entspricht? Ich meine, wirkliche Probleme sehe ich jetzt nur bei der Übergabe an Funktionen, was aber ist das Problem bei meiner &quot;push_backung&quot;??</p>
<blockquote>
<ol>
<li>Fehler bei der Speicherallokation. Der Konstruktor des Objektes wird daher nie aufgerufen, alles paletti (außer dass du ohne Speicher schlecht dastehst)</li>
</ol>
</blockquote>
<p>Heißt das also, es kann passieren, das ich einen Null-Pointer in den Vector pushe, wenn dein Fall auftritt, und wird das push_back bei einem nicht-aufruf des Ctors abbgebrochen, wohl eher nicht,ne?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1642217</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1642217</guid><dc:creator><![CDATA[Firefighter]]></dc:creator><pubDate>Fri, 09 Jan 2009 07:03:06 GMT</pubDate></item></channel></rss>