<?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[Unsicher bzgl. Speicheranforderung und Freigabe]]></title><description><![CDATA[<p>Hallo!</p>
<p>Ich versuche ein Stück OpenCv-Code in C in C++ umzuschreiben.<br />
Hier das Original:</p>
<pre><code class="language-cpp">int npts[2] = { 3, 5 };
          CvPoint **pts;

          pts = (CvPoint **) cvAlloc (sizeof (CvPoint *) * 2);
          pts[0] = (CvPoint *) cvAlloc (sizeof (CvPoint) * 3);
          pts[1] = (CvPoint *) cvAlloc (sizeof (CvPoint) * 5);

          pts[0][0].x = 50;
          pts[0][0].y = 210;
          pts[0][1].x = 10;
          pts[0][1].y = 280;
          pts[0][2].x = 90;
          pts[0][2].y = 285;
          pts[1][0].x = 260;
          pts[1][0].y = 180;
          pts[1][1].x = 198;
          pts[1][1].y = 201;
          pts[1][2].x = 198;
          pts[1][2].y = 270;
          pts[1][3].x = 265;
          pts[1][3].y = 280;
          pts[1][4].x = 290;
          pts[1][4].y = 220;

          cvPolyLine( m_ipl, pts, npts, 2, true,CV_RGB(0,0,255), 2,    8, 0 );
</code></pre>
<p>Hier wird kein Speicher freigegeben und in der Doku von cvPolyLine steht auch nicht, dass er in der Funktion freigegeben wird. Also wäre meine Variante:</p>
<pre><code class="language-cpp">int npts[2] = { 3, 5 };
          CvPoint **pts;

          pts = new CvPoint*[2];           
          pts[0] = new CvPoint[3];
          pts[1] =  new CvPoint[5];

          pts[0][0].x = 50;
          pts[0][0].y = 210;
          pts[0][1].x = 10;
          pts[0][1].y = 280;
          pts[0][2].x = 90;
          pts[0][2].y = 285;
          pts[1][0].x = 260;
          pts[1][0].y = 180;
          pts[1][1].x = 198;
          pts[1][1].y = 201;
          pts[1][2].x = 198;
          pts[1][2].y = 270;
          pts[1][3].x = 265;
          pts[1][3].y = 280;
          pts[1][4].x = 290;
          pts[1][4].y = 220;

          cvPolyLine( m_ipl, pts, npts, 2, true,CV_RGB(0,0,255), 2,  8, 0 );
          delete[] pts[0];
          delete[] pts[1];
          delete[] pts;
</code></pre>
<p>Fordere ich korrekt den Speicher an und gebe ihn so korrekt frei? Danke fürs drüberschauen <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="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/topic/267886/unsicher-bzgl-speicheranforderung-und-freigabe</link><generator>RSS for Node</generator><lastBuildDate>Wed, 02 Sep 2026 06:12:02 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/267886.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 31 May 2010 20:11:42 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Unsicher bzgl. Speicheranforderung und Freigabe on Mon, 31 May 2010 20:11:42 GMT]]></title><description><![CDATA[<p>Hallo!</p>
<p>Ich versuche ein Stück OpenCv-Code in C in C++ umzuschreiben.<br />
Hier das Original:</p>
<pre><code class="language-cpp">int npts[2] = { 3, 5 };
          CvPoint **pts;

          pts = (CvPoint **) cvAlloc (sizeof (CvPoint *) * 2);
          pts[0] = (CvPoint *) cvAlloc (sizeof (CvPoint) * 3);
          pts[1] = (CvPoint *) cvAlloc (sizeof (CvPoint) * 5);

          pts[0][0].x = 50;
          pts[0][0].y = 210;
          pts[0][1].x = 10;
          pts[0][1].y = 280;
          pts[0][2].x = 90;
          pts[0][2].y = 285;
          pts[1][0].x = 260;
          pts[1][0].y = 180;
          pts[1][1].x = 198;
          pts[1][1].y = 201;
          pts[1][2].x = 198;
          pts[1][2].y = 270;
          pts[1][3].x = 265;
          pts[1][3].y = 280;
          pts[1][4].x = 290;
          pts[1][4].y = 220;

          cvPolyLine( m_ipl, pts, npts, 2, true,CV_RGB(0,0,255), 2,    8, 0 );
</code></pre>
<p>Hier wird kein Speicher freigegeben und in der Doku von cvPolyLine steht auch nicht, dass er in der Funktion freigegeben wird. Also wäre meine Variante:</p>
<pre><code class="language-cpp">int npts[2] = { 3, 5 };
          CvPoint **pts;

          pts = new CvPoint*[2];           
          pts[0] = new CvPoint[3];
          pts[1] =  new CvPoint[5];

          pts[0][0].x = 50;
          pts[0][0].y = 210;
          pts[0][1].x = 10;
          pts[0][1].y = 280;
          pts[0][2].x = 90;
          pts[0][2].y = 285;
          pts[1][0].x = 260;
          pts[1][0].y = 180;
          pts[1][1].x = 198;
          pts[1][1].y = 201;
          pts[1][2].x = 198;
          pts[1][2].y = 270;
          pts[1][3].x = 265;
          pts[1][3].y = 280;
          pts[1][4].x = 290;
          pts[1][4].y = 220;

          cvPolyLine( m_ipl, pts, npts, 2, true,CV_RGB(0,0,255), 2,  8, 0 );
          delete[] pts[0];
          delete[] pts[1];
          delete[] pts;
</code></pre>
<p>Fordere ich korrekt den Speicher an und gebe ihn so korrekt frei? Danke fürs drüberschauen <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="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1905188</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1905188</guid><dc:creator><![CDATA[Polymere]]></dc:creator><pubDate>Mon, 31 May 2010 20:11:42 GMT</pubDate></item><item><title><![CDATA[Reply to Unsicher bzgl. Speicheranforderung und Freigabe on Mon, 31 May 2010 20:26:08 GMT]]></title><description><![CDATA[<p>Ja, ist so in Ordnung.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1905193</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1905193</guid><dc:creator><![CDATA[Athar]]></dc:creator><pubDate>Mon, 31 May 2010 20:26:08 GMT</pubDate></item><item><title><![CDATA[Reply to Unsicher bzgl. Speicheranforderung und Freigabe on Tue, 01 Jun 2010 18:28:39 GMT]]></title><description><![CDATA[<p>Polymere schrieb:</p>
<blockquote>
<p>pts = new CvPoint*[2];<br />
pts[0] = new CvPoint[3];<br />
pts[1] = new CvPoint[5];</p>
<p>delete[] pts[0];<br />
delete[] pts[1];<br />
delete[] pts;</p>
<p>Fordere ich korrekt den Speicher an?</p>
</blockquote>
<p>ja</p>
<p>Polymere schrieb:</p>
<blockquote>
<p>und gebe ihn so korrekt frei?</p>
</blockquote>
<p>nein, besser</p>
<pre><code>delete[] pts[1];
          delete[] pts[0];
          delete[] pts;
</code></pre>
<p>Speicherfreigabe immer umgekehrt proportional zur Anforderung. Kann sonst (muß nicht) zu Problemen führen.</p>
<p>tschüß<br />
Troll.Soft</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1905615</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1905615</guid><dc:creator><![CDATA[Troll.Soft]]></dc:creator><pubDate>Tue, 01 Jun 2010 18:28:39 GMT</pubDate></item><item><title><![CDATA[Reply to Unsicher bzgl. Speicheranforderung und Freigabe on Tue, 01 Jun 2010 19:26:48 GMT]]></title><description><![CDATA[<p>Troll.Soft schrieb:</p>
<blockquote>
<p>Speicherfreigabe immer umgekehrt proportional zur Anforderung. Kann sonst (muß nicht) zu Problemen führen.</p>
</blockquote>
<p>Probleme kann es geben, wenn z.B. <code>delete[] pts;</code> als erstes ausgeführt wird. Bei den anderen beiden Freigaben spielt die Reihenfolge keine Rolle.</p>
<p>Wenn man die Möglichkeit hat, rät es sich übrigens, selbst verwaltende Klassen zu verwenden (z.B. <code>std::vector</code> oder hier <code>boost::multi_array</code> ). Dann muss man sich um weniger kümmern und hat einige Probleme nicht.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1905641</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1905641</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Tue, 01 Jun 2010 19:26:48 GMT</pubDate></item></channel></rss>