<?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[Segmentation Fault mit vector]]></title><description><![CDATA[<p>Hallo,</p>
<p>bin verzweifelt auf der Suche nach einem Bug, der bei mir einen segmentation fault auslöst, aber irgendwie steh ich auf dem Schlauch.</p>
<p>Ich hab jetzt mal mein Problem darauf reduziert, einen vector mit 4 elementen zu füllen, und anschließend alle elemente zu löschen (später will ich ein ganz bestimmtes element löschen).</p>
<pre><code class="language-cpp">NodeID nid1 = NodeID(string(&quot;192.168.1.1&quot;),string(&quot;1234&quot;));
	NodeID nid2 = NodeID(string(&quot;192.168.1.2&quot;),string(&quot;765&quot;));
	NodeID nid3 = NodeID(string(&quot;192.168.1.3&quot;),string(&quot;594&quot;));
	NodeID nid4 = NodeID(string(&quot;192.168.1.4&quot;),string(&quot;433&quot;));

	printf(&quot;Nodes created\n&quot;);
	vector&lt;Neighbour&gt; list = vector&lt;Neighbour&gt;();
	list.push_back(nid1);
	list.push_back(nid2);
	list.push_back(nid3);
	list.push_back(nid4);

	vector&lt;Neighbour&gt;::iterator theIterator;

	theIterator = list.begin();
	while(theIterator != list.end()){
printf(&quot;in list, deleting now\n&quot;);
		theIterator = list.erase(theIterator);
		theIterator++;
	}
	printf(&quot;list finished\n&quot;);
</code></pre>
<p>Als Ausgabe bekomme ich :</p>
<pre><code>Nodes created
Neighbour::Neighbour(const NodeID&amp; t_nodeid): Entry
Neighbour::Neighbour(const NodeID&amp; t_nodeid): Exit
Neighbour::Neighbour(const Neighbour&amp; neighbour) 192.168.1.1:1234
Neighbour::~Neighbour(): Entry: 192.168.1.1:1234
Neighbour::Neighbour(const NodeID&amp; t_nodeid): Entry
Neighbour::Neighbour(const NodeID&amp; t_nodeid): Exit
Neighbour::Neighbour(const Neighbour&amp; neighbour) 192.168.1.1:1234
Neighbour::Neighbour(const Neighbour&amp; neighbour) 192.168.1.2:765
Neighbour::~Neighbour(): Entry: 192.168.1.1:1234
Neighbour::~Neighbour(): Entry: 192.168.1.2:765
Neighbour::Neighbour(const NodeID&amp; t_nodeid): Entry
Neighbour::Neighbour(const NodeID&amp; t_nodeid): Exit
Neighbour::Neighbour(const Neighbour&amp; neighbour) 192.168.1.1:1234
Neighbour::Neighbour(const Neighbour&amp; neighbour) 192.168.1.2:765
Neighbour::Neighbour(const Neighbour&amp; neighbour) 192.168.1.3:594
Neighbour::~Neighbour(): Entry: 192.168.1.1:1234
Neighbour::~Neighbour(): Entry: 192.168.1.2:765
Neighbour::~Neighbour(): Entry: 192.168.1.3:594
Neighbour::Neighbour(const NodeID&amp; t_nodeid): Entry
Neighbour::Neighbour(const NodeID&amp; t_nodeid): Exit
Neighbour::Neighbour(const Neighbour&amp; neighbour) 192.168.1.4:433
Neighbour::~Neighbour(): Entry: 192.168.1.4:433
in list, deleting now
Neighbour::~Neighbour(): Entry: 192.168.1.4:433
in list, deleting now
Segmentation fault
</code></pre>
<p>Das heißt er löscht das letzte Element als erstes (warum auch immer) und im nächsten durchlauf taucht dann der seg fault auf. Hab ich irgend etwas vergessen bei der initialisierung des iterators oder so ? Ich kann mir das alles einfach nicht erklären.</p>
<p>Viele Grüße und vielen Dank im Voraus</p>
<p>Jan</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/169837/segmentation-fault-mit-vector</link><generator>RSS for Node</generator><lastBuildDate>Wed, 14 Oct 2026 13:19:26 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/169837.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 09 Jan 2007 13:46:12 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Segmentation Fault mit vector on Tue, 09 Jan 2007 13:46:12 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>bin verzweifelt auf der Suche nach einem Bug, der bei mir einen segmentation fault auslöst, aber irgendwie steh ich auf dem Schlauch.</p>
<p>Ich hab jetzt mal mein Problem darauf reduziert, einen vector mit 4 elementen zu füllen, und anschließend alle elemente zu löschen (später will ich ein ganz bestimmtes element löschen).</p>
<pre><code class="language-cpp">NodeID nid1 = NodeID(string(&quot;192.168.1.1&quot;),string(&quot;1234&quot;));
	NodeID nid2 = NodeID(string(&quot;192.168.1.2&quot;),string(&quot;765&quot;));
	NodeID nid3 = NodeID(string(&quot;192.168.1.3&quot;),string(&quot;594&quot;));
	NodeID nid4 = NodeID(string(&quot;192.168.1.4&quot;),string(&quot;433&quot;));

	printf(&quot;Nodes created\n&quot;);
	vector&lt;Neighbour&gt; list = vector&lt;Neighbour&gt;();
	list.push_back(nid1);
	list.push_back(nid2);
	list.push_back(nid3);
	list.push_back(nid4);

	vector&lt;Neighbour&gt;::iterator theIterator;

	theIterator = list.begin();
	while(theIterator != list.end()){
printf(&quot;in list, deleting now\n&quot;);
		theIterator = list.erase(theIterator);
		theIterator++;
	}
	printf(&quot;list finished\n&quot;);
</code></pre>
<p>Als Ausgabe bekomme ich :</p>
<pre><code>Nodes created
Neighbour::Neighbour(const NodeID&amp; t_nodeid): Entry
Neighbour::Neighbour(const NodeID&amp; t_nodeid): Exit
Neighbour::Neighbour(const Neighbour&amp; neighbour) 192.168.1.1:1234
Neighbour::~Neighbour(): Entry: 192.168.1.1:1234
Neighbour::Neighbour(const NodeID&amp; t_nodeid): Entry
Neighbour::Neighbour(const NodeID&amp; t_nodeid): Exit
Neighbour::Neighbour(const Neighbour&amp; neighbour) 192.168.1.1:1234
Neighbour::Neighbour(const Neighbour&amp; neighbour) 192.168.1.2:765
Neighbour::~Neighbour(): Entry: 192.168.1.1:1234
Neighbour::~Neighbour(): Entry: 192.168.1.2:765
Neighbour::Neighbour(const NodeID&amp; t_nodeid): Entry
Neighbour::Neighbour(const NodeID&amp; t_nodeid): Exit
Neighbour::Neighbour(const Neighbour&amp; neighbour) 192.168.1.1:1234
Neighbour::Neighbour(const Neighbour&amp; neighbour) 192.168.1.2:765
Neighbour::Neighbour(const Neighbour&amp; neighbour) 192.168.1.3:594
Neighbour::~Neighbour(): Entry: 192.168.1.1:1234
Neighbour::~Neighbour(): Entry: 192.168.1.2:765
Neighbour::~Neighbour(): Entry: 192.168.1.3:594
Neighbour::Neighbour(const NodeID&amp; t_nodeid): Entry
Neighbour::Neighbour(const NodeID&amp; t_nodeid): Exit
Neighbour::Neighbour(const Neighbour&amp; neighbour) 192.168.1.4:433
Neighbour::~Neighbour(): Entry: 192.168.1.4:433
in list, deleting now
Neighbour::~Neighbour(): Entry: 192.168.1.4:433
in list, deleting now
Segmentation fault
</code></pre>
<p>Das heißt er löscht das letzte Element als erstes (warum auch immer) und im nächsten durchlauf taucht dann der seg fault auf. Hab ich irgend etwas vergessen bei der initialisierung des iterators oder so ? Ich kann mir das alles einfach nicht erklären.</p>
<p>Viele Grüße und vielen Dank im Voraus</p>
<p>Jan</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1206336</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1206336</guid><dc:creator><![CDATA[Janson]]></dc:creator><pubDate>Tue, 09 Jan 2007 13:46:12 GMT</pubDate></item><item><title><![CDATA[Reply to Segmentation Fault mit vector on Tue, 09 Jan 2007 13:50:06 GMT]]></title><description><![CDATA[<p>Vergisst es, blödsinniger fehler <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>
<p>Jan</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1206339</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1206339</guid><dc:creator><![CDATA[Janson]]></dc:creator><pubDate>Tue, 09 Jan 2007 13:50:06 GMT</pubDate></item><item><title><![CDATA[Reply to Segmentation Fault mit vector on Tue, 09 Jan 2007 13:56:12 GMT]]></title><description><![CDATA[<p>Erstmal gibt erase() einen Iterator auf das erste nicht gelöschte Element zurück, da ist der Inkrement dahinter überflüssig (und führt dich über kurz oder lang aus dem vector raus). Das könntest du mal kontrollieren, indem du in der Schleife jeweils Position &quot;theIterator-list.begin()&quot; und Größe &quot;list.size()&quot; des Vektors ausgibst.</p>
<p>Und zweitens löscht der vector Elemente am Anfang der Sequenz, indem er die Nachfolger nach vorne kopiert und anschließend das letzte Element physikalisch löscht - da könnte also auch noch ein Fehler in deinem operator= stecken.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1206341</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1206341</guid><dc:creator><![CDATA[CStoll]]></dc:creator><pubDate>Tue, 09 Jan 2007 13:56:12 GMT</pubDate></item><item><title><![CDATA[Reply to Segmentation Fault mit vector on Tue, 09 Jan 2007 14:10:02 GMT]]></title><description><![CDATA[<p>Janson schrieb:</p>
<blockquote>
<p>Vergisst es...</p>
</blockquote>
<p>Wer vergisst es ? <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
<p>Gruß,</p>
<p>Simon2.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1206358</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1206358</guid><dc:creator><![CDATA[Simon2]]></dc:creator><pubDate>Tue, 09 Jan 2007 14:10:02 GMT</pubDate></item><item><title><![CDATA[Reply to Segmentation Fault mit vector on Tue, 09 Jan 2007 14:16:40 GMT]]></title><description><![CDATA[<p>Ich behaupte mit Sicherheit, dass die Ursache des Segfault in dem Inkrement hinter dem Löschen liegt <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
<p>1. theIt ist Element 1<br />
2. Element 1 wird gelöscht<br />
3. theIt wird zum Element hinter dem gelöschten (also 2) und nochmal inkrementiert (also 3)<br />
4. Element 3 wird gelöscht<br />
5. theIt wird zum Element hinter dem gelöschten (also end) und nochmal inkrementiert (also &quot;end+1&quot;)<br />
6. Da &quot;end+1&quot; != end ist terminiert die Schleife nicht und -&gt; bumm (wenn nicht schon die Inkrementierung von end zum Segfault führt spätestens der Zugriff auf das Element)</p>
<p>EDIT: Und in 5 liegt ein Denkfehler meinerseits - vergesst es :p</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1206363</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1206363</guid><dc:creator><![CDATA[LordJaxom]]></dc:creator><pubDate>Tue, 09 Jan 2007 14:16:40 GMT</pubDate></item><item><title><![CDATA[Reply to Segmentation Fault mit vector on Tue, 09 Jan 2007 14:28:36 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/10318">@CStoll</a>:</p>
<p>TAUSEND DANK !!!!<br />
Am operator= hats gelegen <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>
<p>Hab an diesen blöden Fehler schon mehrere Tage gehangen.</p>
<p>VIELEN VIELEN DANK !</p>
<p>Viele Grüße</p>
<p>Jan</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1206371</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1206371</guid><dc:creator><![CDATA[Janson]]></dc:creator><pubDate>Tue, 09 Jan 2007 14:28:36 GMT</pubDate></item><item><title><![CDATA[Reply to Segmentation Fault mit vector on Tue, 29 Apr 2008 11:00:40 GMT]]></title><description><![CDATA[<p>Da der Titel soweit passt, mache ich mal hier weiter mit meiner Frage...<br />
Also bei mir kommt auch &quot;Segmentation fault&quot; sobald ich beim 'push_back()' ankomme:</p>
<pre><code class="language-cpp">std::vector&lt;IOModule&gt; toGroup, ioModule;
...
std::cout&lt;&lt;moduleArray.size()&lt;&lt;ioModule.size(); // size = 2

    for(int i=0; i&lt;moduleArray.size(); i++)
                if(rectSelect.intersects(moduleArray[i]))                                         
                   toGroup.push_back(ioModule[i]);//Hier kommt Segmentation fault
</code></pre>
<p>Übersehe ich irgendwas?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1500408</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1500408</guid><dc:creator><![CDATA[Allroad]]></dc:creator><pubDate>Tue, 29 Apr 2008 11:00:40 GMT</pubDate></item><item><title><![CDATA[Reply to Segmentation Fault mit vector on Tue, 29 Apr 2008 11:16:54 GMT]]></title><description><![CDATA[<p>Allroad schrieb:</p>
<blockquote>
<p>Da der Titel soweit passt, mache ich mal hier weiter mit meiner Frage...<br />
Also bei mir kommt auch &quot;Segmentation fault&quot; sobald ich beim 'push_back()' ankomme:</p>
<pre><code class="language-cpp">std::vector&lt;IOModule&gt; toGroup, ioModule;
...
std::cout&lt;&lt;moduleArray.size()&lt;&lt;ioModule.size(); // size = 2

    for(int i=0; i&lt;moduleArray.size(); i++)
                if(rectSelect.intersects(moduleArray[i]))                                         
                   toGroup.push_back(ioModule[i]);//Hier kommt Segmentation fault
</code></pre>
<p>Übersehe ich irgendwas?</p>
</blockquote>
<p>Mehrere Fehlerquellen:<br />
a) Ist moduleArray.size() wirklich immer gleich ioModule.size()?<br />
(wegen Indexzugriff)<br />
b) Ist die Klasse IOModule für Kopien vorgesehen?</p>
<p>Falls du dir unsicher bist solltest du mal mit einem try/catch-Block um die Schleife arbeiten und statt den Indexoperator [i] die Methode at(i) verwenden. Tritt der Fehler dann auch noch auf, oder wurde eine Exception ausgelöst?</p>
<p>cu André</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1500422</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1500422</guid><dc:creator><![CDATA[asc]]></dc:creator><pubDate>Tue, 29 Apr 2008 11:16:54 GMT</pubDate></item><item><title><![CDATA[Reply to Segmentation Fault mit vector on Tue, 29 Apr 2008 11:38:17 GMT]]></title><description><![CDATA[<p>Ok, ich probier das mal aus un melde mich wieder...<br />
zu a) ja, sind immer gleich gross (vom Typ aber verschieden),<br />
zu b) ja, die Klasse hat ein CopyConstr.</p>
<p>aber, es kann sein, dass ich mich geirrt habe und mein Fehler doch von woanders kommt. Bin mir nicht ganz sicher wie ich eindeutig herausfinden kann woher mein Seg. fault kommt...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1500441</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1500441</guid><dc:creator><![CDATA[Allroad]]></dc:creator><pubDate>Tue, 29 Apr 2008 11:38:17 GMT</pubDate></item><item><title><![CDATA[Reply to Segmentation Fault mit vector on Tue, 29 Apr 2008 13:00:38 GMT]]></title><description><![CDATA[<p>So, hab meine Fehler nun gefunden:</p>
<pre><code class="language-cpp">int size = moduleArray.size();
for(int i=0; i&lt;size; i++)
{
if(rectSelect.contains(moduleArray[i], 0)) // Qt-Befehl, nicht weiter interesant, aber an dieser Stelle kommt mein Problem!!!
  moduleArray.removeAt(i); // auch Qt- Befehl zum Entfernen 
         }
</code></pre>
<p>wenn z.B. ich nur zwei Elemente in meinem Vektor habe, wird beim ersten Durchlauf (i=0)das erste Element gelöscht, beim zweiten Durchlauf wird modulArray[1] aufgerufen was nicht mehr existiert...!!! =&gt; Segmentation fault <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="🙄"
    /> Trivial eigentlich..<br />
Dennoch möchte ich irgendwelche Elemente aus meinem Vektor löschen, wie mache ich das am Besten, so dass ich auch die richtige Elemente entferne! (auch wenn ich eine Kopie vom orig.-Vektor mache, sobald ich ein Element rausnehme, ändern sich die Positionen (Indizes) der restlichen Elemente)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1500512</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1500512</guid><dc:creator><![CDATA[Allroad]]></dc:creator><pubDate>Tue, 29 Apr 2008 13:00:38 GMT</pubDate></item></channel></rss>