<?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[RAII und new[]]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich habe eine C-API vorliegen, die viele Funktionen der folgenden Art bereitstellt:</p>
<pre><code class="language-cpp">int foo(some_handle* handles, size_t handle_count);
</code></pre>
<p>Wenn ich nun eine solche Funktion aufrufen moechte, sieht mein Quellcode in etwa so aus:</p>
<pre><code class="language-cpp">void bar(size_t count)
{
  some_handle* handles = new some_handle[count];

  int rc = foo(handles, count);

  // wirft eventuell -&gt; handles nicht freigegeben
  do_something_cpp(handles, count);

  if(rc &lt; 0)
    throw some_error; // handles nicht freigegeben

  delete [] handles;
}
</code></pre>
<p>Jetzt moechte ich mir es ersparen innerhalb eines jeden if-Blocks den Speicher frei zu geben oder Funktionsaufrufe in einen try-catch-Block zu packen (zumal eine moegliche Ausnahme sowieso an den Aufrufer weitergegeben werden muesste).</p>
<p>Ich weiss, dass das richtige Stichwort RAII lautet, aber nicht, welche Klasse man dafuer verwendet (abgesehen von einem selbsgebastelten Lock, ich gehe aber davon aus es existiert schon etwas). Bisher habe ich boost::scoped_array gefunden, passt das? Wie macht man es sonst?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/307641/raii-und-new</link><generator>RSS for Node</generator><lastBuildDate>Thu, 06 Aug 2026 15:50:08 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/307641.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 31 Aug 2012 19:59:47 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to RAII und new[] on Fri, 31 Aug 2012 19:59:47 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich habe eine C-API vorliegen, die viele Funktionen der folgenden Art bereitstellt:</p>
<pre><code class="language-cpp">int foo(some_handle* handles, size_t handle_count);
</code></pre>
<p>Wenn ich nun eine solche Funktion aufrufen moechte, sieht mein Quellcode in etwa so aus:</p>
<pre><code class="language-cpp">void bar(size_t count)
{
  some_handle* handles = new some_handle[count];

  int rc = foo(handles, count);

  // wirft eventuell -&gt; handles nicht freigegeben
  do_something_cpp(handles, count);

  if(rc &lt; 0)
    throw some_error; // handles nicht freigegeben

  delete [] handles;
}
</code></pre>
<p>Jetzt moechte ich mir es ersparen innerhalb eines jeden if-Blocks den Speicher frei zu geben oder Funktionsaufrufe in einen try-catch-Block zu packen (zumal eine moegliche Ausnahme sowieso an den Aufrufer weitergegeben werden muesste).</p>
<p>Ich weiss, dass das richtige Stichwort RAII lautet, aber nicht, welche Klasse man dafuer verwendet (abgesehen von einem selbsgebastelten Lock, ich gehe aber davon aus es existiert schon etwas). Bisher habe ich boost::scoped_array gefunden, passt das? Wie macht man es sonst?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2247497</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2247497</guid><dc:creator><![CDATA[henry_k]]></dc:creator><pubDate>Fri, 31 Aug 2012 19:59:47 GMT</pubDate></item><item><title><![CDATA[Reply to RAII und new[] on Fri, 31 Aug 2012 20:00:21 GMT]]></title><description><![CDATA[<p>std::vector?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2247498</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2247498</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Fri, 31 Aug 2012 20:00:21 GMT</pubDate></item><item><title><![CDATA[Reply to RAII und new[] on Fri, 31 Aug 2012 20:10:39 GMT]]></title><description><![CDATA[<p>SeppJ schrieb:</p>
<blockquote>
<p>std::vector?</p>
</blockquote>
<p>Danke. Wie muss ich dann die C-Funktion korrekt aufrufen, so?</p>
<pre><code class="language-cpp">std::vector&lt;some_handle&gt; handles;

// vector fuellen ...

foo((some_handle*)&amp;handles, count);
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2247503</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2247503</guid><dc:creator><![CDATA[henry_k]]></dc:creator><pubDate>Fri, 31 Aug 2012 20:10:39 GMT</pubDate></item><item><title><![CDATA[Reply to RAII und new[] on Fri, 31 Aug 2012 20:13:59 GMT]]></title><description><![CDATA[<p>henry_k schrieb:</p>
<blockquote>
<p>SeppJ schrieb:</p>
<blockquote>
<p>std::vector?</p>
</blockquote>
<p>Danke. Wie muss ich dann die C-Funktion korrekt aufrufen, so?</p>
<pre><code class="language-cpp">std::vector&lt;some_handle&gt; handles;

// vector fuellen ...

foo((some_handle*)&amp;handles, count);
</code></pre>
</blockquote>
<p>Ach du heilige Scheiße...</p>
<pre><code class="language-cpp">foo(handles.data(), handles.size());
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2247504</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2247504</guid><dc:creator><![CDATA[Sone]]></dc:creator><pubDate>Fri, 31 Aug 2012 20:13:59 GMT</pubDate></item><item><title><![CDATA[Reply to RAII und new[] on Fri, 31 Aug 2012 20:11:25 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">foo(handles.data(), handles.size());
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2247505</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2247505</guid><dc:creator><![CDATA[Kellerautomat]]></dc:creator><pubDate>Fri, 31 Aug 2012 20:11:25 GMT</pubDate></item><item><title><![CDATA[Reply to RAII und new[] on Fri, 31 Aug 2012 20:13:10 GMT]]></title><description><![CDATA[<p>Kellerautomat schrieb:</p>
<blockquote>
<pre><code class="language-cpp">foo(handles.data(), handles.size());
</code></pre>
</blockquote>
<p>Haha <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/2247506</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2247506</guid><dc:creator><![CDATA[Sone]]></dc:creator><pubDate>Fri, 31 Aug 2012 20:13:10 GMT</pubDate></item><item><title><![CDATA[Reply to RAII und new[] on Fri, 31 Aug 2012 20:25:11 GMT]]></title><description><![CDATA[<p>henry_k schrieb:</p>
<blockquote>
<pre><code class="language-cpp">foo((some_handle*)&amp;handles, count);
</code></pre>
</blockquote>
<p>Autsch <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f644.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_rolling_eyes"
      title=":rolling_eyes:"
      alt="🙄"
    /></p>
<p>Unklarheit bestand vor allem darueber, ob man einen Zeiger uebergeben kann und die Elemente dann auch wie in einem Array angeordnet sind, aber das habe ich mittlerweile herausgefunden.</p>
<p>Danke fuer die schnelle Hilfe!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2247510</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2247510</guid><dc:creator><![CDATA[henry_k]]></dc:creator><pubDate>Fri, 31 Aug 2012 20:25:11 GMT</pubDate></item><item><title><![CDATA[Reply to RAII und new[] on Sat, 01 Sep 2012 12:35:33 GMT]]></title><description><![CDATA[<p>So, wenn du nen aktuellen Compiler hast und den Rest von std::vector eh nicht brauchst:</p>
<pre><code class="language-cpp">void bar(size_t count)
{
  std::unique_ptr&lt;some_handle[]&gt; handles(new some_handle[count]);

  int rc = foo(handles, count);
  do_something_cpp(handles, count);
  if(rc &lt; 0)
    throw some_error;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2247657</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2247657</guid><dc:creator><![CDATA[Ethon]]></dc:creator><pubDate>Sat, 01 Sep 2012 12:35:33 GMT</pubDate></item><item><title><![CDATA[Reply to RAII und new[] on Wed, 05 Sep 2012 08:06:58 GMT]]></title><description><![CDATA[<p>Warum geht das nicht?</p>
<pre><code class="language-cpp">void bar(size_t count)
{
  some_handle handles[count];

  int rc = foo(handles, count);

  do_something_cpp(handles, count);

  if(rc &lt; 0)
    throw some_error;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2248569</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2248569</guid><dc:creator><![CDATA[NoNew]]></dc:creator><pubDate>Wed, 05 Sep 2012 08:06:58 GMT</pubDate></item><item><title><![CDATA[Reply to RAII und new[] on Wed, 05 Sep 2012 08:12:22 GMT]]></title><description><![CDATA[<p>NoNew schrieb:</p>
<blockquote>
<p>Warum geht das nicht?</p>
</blockquote>
<p>Weil das in C++ halt nicht erlaubt ist. VLA (Variable Length Arrays) gibts nur in C.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2248572</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2248572</guid><dc:creator><![CDATA[Shade Of Mine]]></dc:creator><pubDate>Wed, 05 Sep 2012 08:12:22 GMT</pubDate></item><item><title><![CDATA[Reply to RAII und new[] on Wed, 05 Sep 2012 08:15:52 GMT]]></title><description><![CDATA[<p>NoNew schrieb:</p>
<blockquote>
<p>Warum geht das nicht?</p>
</blockquote>
<p>Weil count keine Compilezeitkonstante ist. VLAs (variable length arrays) gibt's nur in C. Und in manchen (sprich: einem) bekannten C++-Compilern als unportable Spracherweiterung. Außerdem schleppen VLAs noch einen ganzen Schwanz von Problemen mit sich, die sich mit dem strengeren Typsystem von C++ ganz besonders schlecht vertragen (das heißt aber nicht, dass es in C keine Probleme mit VLAs gäbe). Was ist zum Beispiel der Typ eines solchen Arrays?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2248573</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2248573</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Wed, 05 Sep 2012 08:15:52 GMT</pubDate></item><item><title><![CDATA[Reply to RAII und new[] on Wed, 05 Sep 2012 08:19:07 GMT]]></title><description><![CDATA[<p>*VORDIESTIRNKLATSCH*</p>
<p>Stimmt natürlich. zuviel Java gemacht....</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2248574</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2248574</guid><dc:creator><![CDATA[NoNew]]></dc:creator><pubDate>Wed, 05 Sep 2012 08:19:07 GMT</pubDate></item><item><title><![CDATA[Reply to RAII und new[] on Wed, 05 Sep 2012 08:44:56 GMT]]></title><description><![CDATA[<p>Java kann das erst recht nicht, da muss jedes Array mit new angelegt werden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2248585</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2248585</guid><dc:creator><![CDATA[Ethon]]></dc:creator><pubDate>Wed, 05 Sep 2012 08:44:56 GMT</pubDate></item><item><title><![CDATA[Reply to RAII und new[] on Wed, 05 Sep 2012 09:25:42 GMT]]></title><description><![CDATA[<p>Ethon schrieb:</p>
<blockquote>
<p>So, wenn du nen aktuellen Compiler hast und den Rest von</p>
</blockquote>
<p>Fehlt da nicht irgendwie ein <code>unique_ptr::get</code> ?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2248612</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2248612</guid><dc:creator><![CDATA[Tachyon]]></dc:creator><pubDate>Wed, 05 Sep 2012 09:25:42 GMT</pubDate></item></channel></rss>