<?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[dynamische Arrays]]></title><description><![CDATA[<p>Hallo zusammen,</p>
<p>ich bin noch Neuling und habe ein Problem.</p>
<p>Ich habe eine Struktur, die einen Zeiger auf ein Array mit integer-Werten enthält, sowie die Größe des Arrays.</p>
<pre><code>struct dynArr {
	int *vek;
	int anz;
}dynArray;
</code></pre>
<p>In einer init-Funktion erstelle ich das Array mit new und schreibe die Zahlen 1 bis anzahl hinein:</p>
<pre><code>void initStruct (struct dynArr &amp;dynArray, int anz)
{
	dynArray.anz = anz;
	delete dynArray.vek;
	dynArray.vek = NULL;
	dynArray.vek = new int [anz];

	for (int i=0;i&lt;anz;i++)
	{
		dynArray.vek[i] = i+1;

	}
}
</code></pre>
<p>Jetzt möchte ich ein Element löschen und das Array verkürzen, also auch den Speicher wieder freigeben.</p>
<pre><code>void loesche (struct dynArr &amp;dynArray, int pos)
{
	if (dynArray.vek==NULL) return;
	if (dynArray.anz&lt;pos) return;
	int *tmp = new int [dynArray.anz-1];
	for (int i=0;i&lt;dynArray.anz;i++)
	{
		if (i&lt;pos) tmp[i] = dynArray.vek[i];
		if (pos&gt;i) tmp[i-1]=dynArray.vek[i];
	}
	delete [] dynArray.vek;
	dynArray.vek = tmp;
	dynArray.anz--;
</code></pre>
<p>Beim delete steigt er aber leider aus. Weiß jemand, was ich falsch mache ?</p>
<p>Danke und Gruß, Senifor</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/312505/dynamische-arrays</link><generator>RSS for Node</generator><lastBuildDate>Sun, 02 Aug 2026 18:46:33 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/312505.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 06 Jan 2013 17:08:25 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to dynamische Arrays on Sun, 06 Jan 2013 17:08:25 GMT]]></title><description><![CDATA[<p>Hallo zusammen,</p>
<p>ich bin noch Neuling und habe ein Problem.</p>
<p>Ich habe eine Struktur, die einen Zeiger auf ein Array mit integer-Werten enthält, sowie die Größe des Arrays.</p>
<pre><code>struct dynArr {
	int *vek;
	int anz;
}dynArray;
</code></pre>
<p>In einer init-Funktion erstelle ich das Array mit new und schreibe die Zahlen 1 bis anzahl hinein:</p>
<pre><code>void initStruct (struct dynArr &amp;dynArray, int anz)
{
	dynArray.anz = anz;
	delete dynArray.vek;
	dynArray.vek = NULL;
	dynArray.vek = new int [anz];

	for (int i=0;i&lt;anz;i++)
	{
		dynArray.vek[i] = i+1;

	}
}
</code></pre>
<p>Jetzt möchte ich ein Element löschen und das Array verkürzen, also auch den Speicher wieder freigeben.</p>
<pre><code>void loesche (struct dynArr &amp;dynArray, int pos)
{
	if (dynArray.vek==NULL) return;
	if (dynArray.anz&lt;pos) return;
	int *tmp = new int [dynArray.anz-1];
	for (int i=0;i&lt;dynArray.anz;i++)
	{
		if (i&lt;pos) tmp[i] = dynArray.vek[i];
		if (pos&gt;i) tmp[i-1]=dynArray.vek[i];
	}
	delete [] dynArray.vek;
	dynArray.vek = tmp;
	dynArray.anz--;
</code></pre>
<p>Beim delete steigt er aber leider aus. Weiß jemand, was ich falsch mache ?</p>
<p>Danke und Gruß, Senifor</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2286865</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2286865</guid><dc:creator><![CDATA[Senifor]]></dc:creator><pubDate>Sun, 06 Jan 2013 17:08:25 GMT</pubDate></item><item><title><![CDATA[Reply to dynamische Arrays on Sun, 06 Jan 2013 17:11:30 GMT]]></title><description><![CDATA[<p>Das was du da schreibst ist C mit new und delete.<br />
In C++ gibt es Klassen dafür. Und müsste der Compiler nicht bei struct dynArr &amp;dynArray meckern?<br />
Und was meinst du mit &quot;bei delete steigt der aus&quot;, Absturz?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2286869</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2286869</guid><dc:creator><![CDATA[Nathan]]></dc:creator><pubDate>Sun, 06 Jan 2013 17:11:30 GMT</pubDate></item><item><title><![CDATA[Reply to dynamische Arrays on Sun, 06 Jan 2013 17:12:33 GMT]]></title><description><![CDATA[<p>Ist das irgendwiw vorgegeben, oder warum willst du das in <code>C++</code> ausgerechnet so machen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2286870</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2286870</guid><dc:creator><![CDATA[ScottZhang]]></dc:creator><pubDate>Sun, 06 Jan 2013 17:12:33 GMT</pubDate></item><item><title><![CDATA[Reply to dynamische Arrays on Sun, 06 Jan 2013 17:19:12 GMT]]></title><description><![CDATA[<p>Arrays, die mit new[] erstellt wurden, werden mit delete[] gelöscht, nicht mit delete. Und wer garantiert dir, dass dynArray.vek in der init()-Funktion bereits auf etwas gültiges zeigt?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2286877</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2286877</guid><dc:creator><![CDATA[Athar]]></dc:creator><pubDate>Sun, 06 Jan 2013 17:19:12 GMT</pubDate></item><item><title><![CDATA[Reply to dynamische Arrays on Sun, 06 Jan 2013 17:30:23 GMT]]></title><description><![CDATA[<p>Danke für die Hilfe. Bin noch am Lernen ...</p>
<p>Ja, es war vorgegeben, das mit einer Struktur und dynamischem Array zu realisieren. Deshalb habe ich z.B. keine Vektorklasse verwendet ....</p>
<p>Bei der Ausführung des Programms zeigt er den Inhalt des Arrays nach der init-Funktion korrekt an, aber bei dem delete in der &quot;loesche&quot;-Funktion kommt folgende Ausgabe:</p>
<pre><code>0231e000-0233f000 rw-p 00000000 00:00 0                                  [heap]
7f01c0500000-7f01c0515000 r-xp 00000000 fc:01 4456518                    /lib/x86_64-linux-gnu/libgcc_s.so.1
7f01c0515000-7f01c0714000 ---p 00015000 fc:01 4456518                    /lib/x86_64-linux-gnu/libgcc_s.so.1
7f01c0714000-7f01c0715000 r--p 00014000 fc:01 4456518                    /lib/x86_64-linux-gnu/libgcc_s.so.1
7f01c0715000-7f01c0716000 rw-p 00015000 fc:01 4456518                    /lib/x86_64-linux-gnu/libgcc_s.so.1
7f01c0716000-7f01c0811000 r-xp 00000000 fc:01 4462481                    /lib/x86_64-linux-gnu/libm-2.15.so
7f01c0811000-7f01c0a10000 ---p 000fb000 fc:01 4462481                    /lib/x86_64-linux-gnu/libm-2.15.so
7f01c0a10000-7f01c0a11000 r--p 000fa000 fc:01 4462481                    /lib/x86_64-linux-gnu/libm-2.15.so
7f01c0a11000-7f01c0a12000 rw-p 000fb000 fc:01 4462481                    /lib/x86_64-linux-gnu/libm-2.15.so
7f01c0a12000-7f01c0bc7000 r-xp 00000000 fc:01 4462473                    /lib/x86_64-linux-gnu/libc-2.15.so
7f01c0bc7000-7f01c0dc6000 ---p 001b5000 fc:01 4462473                    /lib/x86_64-linux-gnu/libc-2.15.so
7f01c0dc6000-7f01c0dca000 r--p 001b4000 fc:01 4462473                    /lib/x86_64-linux-gnu/libc-2.15.so
7f01c0dca000-7f01c0dcc000 rw-p 001b8000 fc:01 4462473                    /lib/x86_64-linux-gnu/libc-2.15.so
7f01c0dcc000-7f01c0dd1000 rw-p 00000000 00:00 0 
7f01c0dd1000-7f01c0eb3000 r-xp 00000000 fc:01 5243560                    /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16
7f01c0eb3000-7f01c10b2000 ---p 000e2000 fc:01 5243560                    /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16
7f01c10b2000-7f01c10ba000 r--p 000e1000 fc:01 5243560                    /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16
7f01c10ba000-7f01c10bc000 rw-p 000e9000 fc:01 5243560                    /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16
7f01c10bc000-7f01c10d1000 rw-p 00000000 00:00 0 
7f01c10d1000-7f01c10f3000 r-xp 00000000 fc:01 4462487                    /lib/x86_64-linux-gnu/ld-2.15.so
7f01c12c7000-7f01c12cc000 rw-p 00000000 00:00 0 
7f01c12ef000-7f01c12f3000 rw-p 00000000 00:00 0 
7f01c12f3000-7f01c12f4000 r--p 00022000 fc:01 4462487                    /lib/x86_64-linux-gnu/ld-2.15.so
7f01c12f4000-7f01c12f6000 rw-p 00023000 fc:01 4462487                    /lib/x86_64-linux-gnu/ld-2.15.so
7fff961eb000-7fff9620c000 rw-p 00000000 00:00 0                          [stack]
7fff96332000-7fff96333000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
</code></pre>
<p>bei struct dynArr &amp;dynArray meckert er nicht. Compiliert wird es ohne Fehler.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2286891</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2286891</guid><dc:creator><![CDATA[Senifor]]></dc:creator><pubDate>Sun, 06 Jan 2013 17:30:23 GMT</pubDate></item><item><title><![CDATA[Reply to dynamische Arrays on Sun, 06 Jan 2013 17:37:12 GMT]]></title><description><![CDATA[<p>Ich denke es liegt nicht an dem delete [] sondern an der Schleife davor.<br />
Dir ist schon klar das if (i&lt;pos) und if (pos&gt;i) genau dasselbe ist?<br />
Deswegen werden werten an verk[-1] verändert, dass führt zu zerstörtem Speicher.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2286895</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2286895</guid><dc:creator><![CDATA[Nathan]]></dc:creator><pubDate>Sun, 06 Jan 2013 17:37:12 GMT</pubDate></item><item><title><![CDATA[Reply to dynamische Arrays on Sun, 06 Jan 2013 17:41:01 GMT]]></title><description><![CDATA[<p>ähm *hüst* ja. Das war es ... es muss pos&lt;i heissen. <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="🙄"
    /><br />
Sorry für die Umstände. Ich dachte ich mach beim Freigeben, was falsch.<br />
Jetzt funktioniert es.<br />
Danke !</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2286899</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2286899</guid><dc:creator><![CDATA[Senifor]]></dc:creator><pubDate>Sun, 06 Jan 2013 17:41:01 GMT</pubDate></item><item><title><![CDATA[Reply to dynamische Arrays on Sun, 06 Jan 2013 17:42:25 GMT]]></title><description><![CDATA[<p>Das bezweifle ich, denn folgendes ist immer noch nicht geklärt:</p>
<p>Athar schrieb:</p>
<blockquote>
<p>Arrays, die mit new[] erstellt wurden, werden mit delete[] gelöscht, nicht mit delete. Und wer garantiert dir, dass dynArray.vek in der init()-Funktion bereits auf etwas gültiges zeigt?</p>
</blockquote>
<p>Außerdem scheint mir diese Zeile auch nicht richtig zu sein:</p>
<pre><code class="language-cpp">if (dynArray.anz&lt;pos) return;
</code></pre>
<p>Auch anz ist keine gültige Position.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2286901</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2286901</guid><dc:creator><![CDATA[Athar]]></dc:creator><pubDate>Sun, 06 Jan 2013 17:42:25 GMT</pubDate></item><item><title><![CDATA[Reply to dynamische Arrays on Sun, 06 Jan 2013 17:59:50 GMT]]></title><description><![CDATA[<p>In der Funktion initStruct meckert er nicht bei delete .... Weiß nicht warum ??<br />
Das andere hab ich geändert geändert:</p>
<pre><code>if (dynArray.anz&lt;=pos) return;
</code></pre>
<p>und die int-werte als unsigned</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2286916</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2286916</guid><dc:creator><![CDATA[Senifor]]></dc:creator><pubDate>Sun, 06 Jan 2013 17:59:50 GMT</pubDate></item><item><title><![CDATA[Reply to dynamische Arrays on Sun, 06 Jan 2013 17:57:16 GMT]]></title><description><![CDATA[<p>Nur mal so aus Neugier:<br />
Da du in initStruct etwas löschst, musst du auch Speicher angefordert haben, oder?<br />
Kannst du uns mal zeigen wo und wie?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2286921</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2286921</guid><dc:creator><![CDATA[Nathan]]></dc:creator><pubDate>Sun, 06 Jan 2013 17:57:16 GMT</pubDate></item><item><title><![CDATA[Reply to dynamische Arrays on Sun, 06 Jan 2013 18:00:51 GMT]]></title><description><![CDATA[<p>nein, habe keinen Speicher angefordert zu dem Zeitpunkt.<br />
Also ist das delete und Null sinnlos oder ?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2286926</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2286926</guid><dc:creator><![CDATA[Senifor]]></dc:creator><pubDate>Sun, 06 Jan 2013 18:00:51 GMT</pubDate></item><item><title><![CDATA[Reply to dynamische Arrays on Sun, 06 Jan 2013 18:02:35 GMT]]></title><description><![CDATA[<p>Ja.<br />
Und um falsche Zugriffe zu vermeiden gibt es in C++ Zugriffsschutz...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2286929</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2286929</guid><dc:creator><![CDATA[Nathan]]></dc:creator><pubDate>Sun, 06 Jan 2013 18:02:35 GMT</pubDate></item><item><title><![CDATA[Reply to dynamische Arrays on Sun, 06 Jan 2013 18:08:25 GMT]]></title><description><![CDATA[<p>ok. Danke.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2286934</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2286934</guid><dc:creator><![CDATA[Senifor]]></dc:creator><pubDate>Sun, 06 Jan 2013 18:08:25 GMT</pubDate></item></channel></rss>