<?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[Deallocation von rekursiv erstellten Objekten]]></title><description><![CDATA[<p>Hallo zusammen</p>
<p>Ich habe ein Programm geschrieben zum berechnen eines N-Körper Problems (2D) mit der Fast Multipole Method. Dabei wird die Quadratische Welt sukzesive in 4 glech grosse Quadrate (Box) geteilt, bis in jeder Box nur noch Nmax Partikel sind.</p>
<p>Das Programm funktioniert, jedoch habe ich das Problem, dass es sehr viel Speicher benötigt. Für 1e5 Iterationsschritte (die Teilung der Welt wird bei jedem Iterationsschritt neu ausgeführt =&gt; &gt;200 Boxen pro Schritt) benötigte das Programm ca. 3GB Speicher!</p>
<p>Ich glaube es liegt daran dass die mit</p>
<pre><code class="language-cpp">new
</code></pre>
<p>erstellten Boxen nicht wieder freigegeben werden.</p>
<p>Der dazu relevante Code ist unten angehängt.</p>
<pre><code class="language-cpp">struct Box {
//  Members:
    bool bEmpty;            // true if leaf node
    std::complex&lt;float&gt; a[8];   // Multipole expansions of the Box-center
    Box *Parent;
    Box *Children[4];       // Boxes of the 4 children
    float width;            // size of box
    std::complex&lt;float&gt; center;
    unsigned int idx_start; // start of particles in data-vector
    unsigned int Np; // number of particles in Box

//  Constructor:
    Box(Box*,  bool , float , float* , unsigned int , unsigned int);
    ~Box();
//  Split particles in Boxes and recursively create boxes:    
    void Split(std::vector&lt;Box*&gt;&amp;,std::vector&lt;Particle&gt; &amp;, const int);
};

////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Box::Split(/*....*/) {
   // ...... some code......... 
            for (int i=0;i&lt;4;i++) {

                    // create children-box:
                    Children[i] = new Box(this-&gt;Parent,false, width*0.5, centerChild,  idx_s, NpChild[i]);

                    // split data in children-box again:
                    Children[i] -&gt; Split(/*.....*/);
            }
    }//end Split()
</code></pre>
<p>Ist es notwendig dass die mit &quot;new&quot; erstellten Boxen wieder gelöscht werden?<br />
Wie macht man das? die stl::vector lösche ich mit .clear(), wie macht man das für die rekursiv erstellten Objekte (Box)?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/275528/deallocation-von-rekursiv-erstellten-objekten</link><generator>RSS for Node</generator><lastBuildDate>Wed, 26 Aug 2026 23:24:01 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/275528.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 16 Oct 2010 10:17:02 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Deallocation von rekursiv erstellten Objekten on Sat, 16 Oct 2010 11:04:34 GMT]]></title><description><![CDATA[<p>Hallo zusammen</p>
<p>Ich habe ein Programm geschrieben zum berechnen eines N-Körper Problems (2D) mit der Fast Multipole Method. Dabei wird die Quadratische Welt sukzesive in 4 glech grosse Quadrate (Box) geteilt, bis in jeder Box nur noch Nmax Partikel sind.</p>
<p>Das Programm funktioniert, jedoch habe ich das Problem, dass es sehr viel Speicher benötigt. Für 1e5 Iterationsschritte (die Teilung der Welt wird bei jedem Iterationsschritt neu ausgeführt =&gt; &gt;200 Boxen pro Schritt) benötigte das Programm ca. 3GB Speicher!</p>
<p>Ich glaube es liegt daran dass die mit</p>
<pre><code class="language-cpp">new
</code></pre>
<p>erstellten Boxen nicht wieder freigegeben werden.</p>
<p>Der dazu relevante Code ist unten angehängt.</p>
<pre><code class="language-cpp">struct Box {
//  Members:
    bool bEmpty;            // true if leaf node
    std::complex&lt;float&gt; a[8];   // Multipole expansions of the Box-center
    Box *Parent;
    Box *Children[4];       // Boxes of the 4 children
    float width;            // size of box
    std::complex&lt;float&gt; center;
    unsigned int idx_start; // start of particles in data-vector
    unsigned int Np; // number of particles in Box

//  Constructor:
    Box(Box*,  bool , float , float* , unsigned int , unsigned int);
    ~Box();
//  Split particles in Boxes and recursively create boxes:    
    void Split(std::vector&lt;Box*&gt;&amp;,std::vector&lt;Particle&gt; &amp;, const int);
};

////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Box::Split(/*....*/) {
   // ...... some code......... 
            for (int i=0;i&lt;4;i++) {

                    // create children-box:
                    Children[i] = new Box(this-&gt;Parent,false, width*0.5, centerChild,  idx_s, NpChild[i]);

                    // split data in children-box again:
                    Children[i] -&gt; Split(/*.....*/);
            }
    }//end Split()
</code></pre>
<p>Ist es notwendig dass die mit &quot;new&quot; erstellten Boxen wieder gelöscht werden?<br />
Wie macht man das? die stl::vector lösche ich mit .clear(), wie macht man das für die rekursiv erstellten Objekte (Box)?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1966359</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1966359</guid><dc:creator><![CDATA[kater k]]></dc:creator><pubDate>Sat, 16 Oct 2010 11:04:34 GMT</pubDate></item><item><title><![CDATA[Reply to Deallocation von rekursiv erstellten Objekten on Sat, 16 Oct 2010 10:30:57 GMT]]></title><description><![CDATA[<p>Mit <code>new</code> erstellte Objekte werden mit <code>delete</code> gelöscht, bei C-Arrays muss man <code>delete[]</code> benutzen.<br />
Alternativ kann man Smart-pointers einsetzen. Es gibt dazu einen Artikel im Magazin hier: <a href="http://magazin.c-plusplus.net/artikel/Schlaue%20Zeiger%20-%20Boost%20Smart%20Pointer" rel="nofollow">http://magazin.c-plusplus.net/artikel/Schlaue%20Zeiger%20-%20Boost%20Smart%20Pointer</a>. <code>shared_ptr</code> und <code>weak_ptr</code> sind bei neueren Compilern auch nach <code>#include &lt;memory&gt;</code> unter <code>std::tr1::</code> vorhanden.<br />
EDIT: Siehe auch <a href="http://www.cplusplus.com/doc/tutorial/dynamic/" rel="nofollow">http://www.cplusplus.com/doc/tutorial/dynamic/</a>, Absatz &quot;Operators delete and delete[]&quot;</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1966363</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1966363</guid><dc:creator><![CDATA[Oberon_0]]></dc:creator><pubDate>Sat, 16 Oct 2010 10:30:57 GMT</pubDate></item><item><title><![CDATA[Reply to Deallocation von rekursiv erstellten Objekten on Sat, 16 Oct 2010 10:30:42 GMT]]></title><description><![CDATA[<p><a href="http://en.wikipedia.org/wiki/Quadtree" rel="nofollow">http://en.wikipedia.org/wiki/Quadtree</a> ?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1966366</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1966366</guid><dc:creator><![CDATA[padreigh]]></dc:creator><pubDate>Sat, 16 Oct 2010 10:30:42 GMT</pubDate></item><item><title><![CDATA[Reply to Deallocation von rekursiv erstellten Objekten on Sat, 16 Oct 2010 11:03:43 GMT]]></title><description><![CDATA[<p>padreigh, ja die Box Struktur ist ein Quadtree.</p>
<p>Oberon_0, dann müsste ich jedes <em>Children</em> mit <em>delete</em> löschen können. Also habe ich dies versucht:</p>
<pre><code class="language-cpp">Box::~Box(){
    for (int i=0;i&lt;4;i++) {
        if (Children[i] != NULL) {
            Children[i]-&gt;~Box();
            delete Children[i];
        }

    }
}
</code></pre>
<p>Der Versuch dies für die top-level Box rootBox.~Box() aufzurufen führte zum Absturz des Programms.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1966376</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1966376</guid><dc:creator><![CDATA[kater k]]></dc:creator><pubDate>Sat, 16 Oct 2010 11:03:43 GMT</pubDate></item><item><title><![CDATA[Reply to Deallocation von rekursiv erstellten Objekten on Sat, 16 Oct 2010 14:49:12 GMT]]></title><description><![CDATA[<p>Nach langem studieren der Literatur <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /> und einigem Ausprobieren habe ich die Lösung gefunden:</p>
<pre><code class="language-cpp">Box::~Box(){
    for (int i=0;i&lt;4;i++) {
            delete Children[i];
            Children[i]=NULL;
    }
}
</code></pre>
<p>Dies wird für die rootBox automatisch bei beendigung der Funktion ausgeführt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1966431</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1966431</guid><dc:creator><![CDATA[kater k]]></dc:creator><pubDate>Sat, 16 Oct 2010 14:49:12 GMT</pubDate></item></channel></rss>