<?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[Absturz bei std::vector-&amp;gt;erase()]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich habe folgende Funktion:</p>
<pre><code class="language-cpp">ivec4 Atlas::getRegion(const size_t width, const size_t height)
	{
		[...]
		ivec3 *node, *prev;
		ivec4 region = {{0, 0, width, height}};
		std::vector&lt;ivec3&gt;::iterator i, bestIndex;

		[...]
		bestIndex = this-&gt;nodes-&gt;begin() - 1;
		[...]

		for(	i = this-&gt;nodes-&gt;begin();
				i != this-&gt;nodes-&gt;end();
				++i )
		{
			y = this-&gt;fit(i, width, height);
			if( y &gt;= 0)
			{
				node = &amp;*i;
				if( (y + height) &lt; (bestHeight) || ( ( (y + height) == bestHeight) &amp;&amp; (node-&gt;z &lt; bestWidth) ) )
				{
					bestHeight = y + height;
					bestIndex = i;
					bestWidth = node-&gt;z;
					region.x = node-&gt;x;
					region.y = node-&gt;y;
				}
			}
		}

		if( bestIndex == this-&gt;nodes-&gt;begin()+1 )
		{
			region.x		= -1;
			region.y		= -1;
			region.width	= 0;
			region.height	= 0;
			return region;
		}

		node-&gt;x	= region.x;
		node-&gt;y	= region.y;
		node-&gt;z = width;
		this-&gt;nodes-&gt;insert(bestIndex, *node);

		for(i = bestIndex + 1; i != this-&gt;nodes-&gt;end();)
		{
			node = &amp;*i;
			prev = &amp;*(i-1);

			if(node-&gt;x &lt; (prev-&gt;x + prev-&gt;z))
			{
				int shrink = prev-&gt;x + prev-&gt;z - node-&gt;x;
				node-&gt;x += shrink;
				node-&gt;z -= shrink;

				if(node-&gt;z &lt;= 0)
				{
					i = this-&gt;nodes-&gt;erase(i); // Absturz
					--i;
				}
				else
				{
					++i;
					break;
				}
			}
			else
				break;
		}
		return region;
	}
</code></pre>
<p>Definition ivec3:</p>
<pre><code class="language-cpp">typedef union
{
	int data[3];
	int rgb[3];
	int xyz[3];
	int xy[2];
	struct { int x;     int y;      int z;     };
	struct { int width; int height; int depth; };
	struct { int r;     int g;      int b;     };
	struct { int red;   int green;  int blue;  };
} ivec3;
</code></pre>
<p>Definition ivec4:</p>
<pre><code class="language-cpp">typedef union
{
	int data[4];
	int xyzw[4];
	int rgba[4];
	int rgb[3];
	int xyz[3];
	struct { int x;   int y;     int z;     int w;      };
	struct { int x_;  int y_;    int width; int height; };
	struct { int r;   int g;     int b;     int a;      };
	struct { int red; int green; int blue;  int alpha;  };
} ivec4;
</code></pre>
<p>Definition this-&gt;nodes:</p>
<pre><code class="language-cpp">std::vector&lt;ivec3&gt; *nodes;
</code></pre>
<p>Mein Problem ist nun, dass mein Programm an der kommentierten Stelle abstürzt (--i wird nicht mehr aufgerufen). Eine Internetrecherche zu dem Thema hat nichts ergeben, ebenso die Forensuche. I.d.R. haben die Leute den Iterator in der for-Schleife inkrementiert, das trifft in meinem Fall aber nicht zu. Irgendwelche Ideen? Danke schonmal im Voraus.</p>
<p>MfG</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/302819/absturz-bei-std-vector-gt-erase</link><generator>RSS for Node</generator><lastBuildDate>Tue, 11 Aug 2026 02:01:42 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/302819.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 27 Apr 2012 19:30:47 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Absturz bei std::vector-&amp;gt;erase() on Fri, 27 Apr 2012 19:30:47 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich habe folgende Funktion:</p>
<pre><code class="language-cpp">ivec4 Atlas::getRegion(const size_t width, const size_t height)
	{
		[...]
		ivec3 *node, *prev;
		ivec4 region = {{0, 0, width, height}};
		std::vector&lt;ivec3&gt;::iterator i, bestIndex;

		[...]
		bestIndex = this-&gt;nodes-&gt;begin() - 1;
		[...]

		for(	i = this-&gt;nodes-&gt;begin();
				i != this-&gt;nodes-&gt;end();
				++i )
		{
			y = this-&gt;fit(i, width, height);
			if( y &gt;= 0)
			{
				node = &amp;*i;
				if( (y + height) &lt; (bestHeight) || ( ( (y + height) == bestHeight) &amp;&amp; (node-&gt;z &lt; bestWidth) ) )
				{
					bestHeight = y + height;
					bestIndex = i;
					bestWidth = node-&gt;z;
					region.x = node-&gt;x;
					region.y = node-&gt;y;
				}
			}
		}

		if( bestIndex == this-&gt;nodes-&gt;begin()+1 )
		{
			region.x		= -1;
			region.y		= -1;
			region.width	= 0;
			region.height	= 0;
			return region;
		}

		node-&gt;x	= region.x;
		node-&gt;y	= region.y;
		node-&gt;z = width;
		this-&gt;nodes-&gt;insert(bestIndex, *node);

		for(i = bestIndex + 1; i != this-&gt;nodes-&gt;end();)
		{
			node = &amp;*i;
			prev = &amp;*(i-1);

			if(node-&gt;x &lt; (prev-&gt;x + prev-&gt;z))
			{
				int shrink = prev-&gt;x + prev-&gt;z - node-&gt;x;
				node-&gt;x += shrink;
				node-&gt;z -= shrink;

				if(node-&gt;z &lt;= 0)
				{
					i = this-&gt;nodes-&gt;erase(i); // Absturz
					--i;
				}
				else
				{
					++i;
					break;
				}
			}
			else
				break;
		}
		return region;
	}
</code></pre>
<p>Definition ivec3:</p>
<pre><code class="language-cpp">typedef union
{
	int data[3];
	int rgb[3];
	int xyz[3];
	int xy[2];
	struct { int x;     int y;      int z;     };
	struct { int width; int height; int depth; };
	struct { int r;     int g;      int b;     };
	struct { int red;   int green;  int blue;  };
} ivec3;
</code></pre>
<p>Definition ivec4:</p>
<pre><code class="language-cpp">typedef union
{
	int data[4];
	int xyzw[4];
	int rgba[4];
	int rgb[3];
	int xyz[3];
	struct { int x;   int y;     int z;     int w;      };
	struct { int x_;  int y_;    int width; int height; };
	struct { int r;   int g;     int b;     int a;      };
	struct { int red; int green; int blue;  int alpha;  };
} ivec4;
</code></pre>
<p>Definition this-&gt;nodes:</p>
<pre><code class="language-cpp">std::vector&lt;ivec3&gt; *nodes;
</code></pre>
<p>Mein Problem ist nun, dass mein Programm an der kommentierten Stelle abstürzt (--i wird nicht mehr aufgerufen). Eine Internetrecherche zu dem Thema hat nichts ergeben, ebenso die Forensuche. I.d.R. haben die Leute den Iterator in der for-Schleife inkrementiert, das trifft in meinem Fall aber nicht zu. Irgendwelche Ideen? Danke schonmal im Voraus.</p>
<p>MfG</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2206218</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2206218</guid><dc:creator><![CDATA[Dengar]]></dc:creator><pubDate>Fri, 27 Apr 2012 19:30:47 GMT</pubDate></item><item><title><![CDATA[Reply to Absturz bei std::vector-&amp;gt;erase() on Fri, 27 Apr 2012 19:39:00 GMT]]></title><description><![CDATA[<p>1. Debuggt?<br />
2. <code>this-&gt;</code> kannst du weglassen,<br />
4. und sieh mal ob das eine gültige Aktion ist, was du da machst (denn vielleicht zeigt <code>i</code> ja auf <code>end()</code> , usw. hab mir den Code nicht ganz durchgelesen)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2206220</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2206220</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Fri, 27 Apr 2012 19:39:00 GMT</pubDate></item><item><title><![CDATA[Reply to Absturz bei std::vector-&amp;gt;erase() on Fri, 27 Apr 2012 20:38:44 GMT]]></title><description><![CDATA[<p>Hacker schrieb:</p>
<blockquote>
<p>1. Debuggt?</p>
</blockquote>
<p>Hätte ich mal machen sollen, bevor ich das hier einstelle... Offenbar ist schon im Vorfeld in anderen Funktionen einiges schiefgelaufen.</p>
<blockquote>
<ol start="2">
<li><code>this-&gt;</code> kannst du weglassen,</li>
</ol>
</blockquote>
<p>Weiß ich. Es ist (meiner Meinung nach) so aber übersichtlicher.</p>
<blockquote>
<p>4. und sieh mal ob das eine gültige Aktion ist, was du da machst (denn vielleicht zeigt <code>i</code> ja auf <code>end()</code> , usw. hab mir den Code nicht ganz durchgelesen)</p>
</blockquote>
<p>Siehe oben. Ich muss erstmal die Fehler vorher untersuchen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2206231</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2206231</guid><dc:creator><![CDATA[Dengar]]></dc:creator><pubDate>Fri, 27 Apr 2012 20:38:44 GMT</pubDate></item><item><title><![CDATA[Reply to Absturz bei std::vector-&amp;gt;erase() on Fri, 27 Apr 2012 21:14:55 GMT]]></title><description><![CDATA[<p>Also, ich konnte die Fehler außerhalb der Funktion abstellen. Problematisch wird es jetzt wieder innerhalb, und zwar an dieser Stelle:</p>
<pre><code class="language-cpp">[...]
	this-&gt;nodes-&gt;insert(bestIndex, *node);

		for(	i = bestIndex + 1;
				i != this-&gt;nodes-&gt;end();)
		{
			node = &amp;*i;
			prev = &amp;*(i-1);
			[...]
</code></pre>
<p>Genauer gesagt im Kopf der for-Schleife. Der Debugger wirft hier &quot;vector iterators incompatible&quot; aus, offenbar wird einer der beiden Iteratoren vorher verändert. Mir ist aber nicht ganz klar, wo in der Funktion das geschehen sollte.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2206241</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2206241</guid><dc:creator><![CDATA[Dengar]]></dc:creator><pubDate>Fri, 27 Apr 2012 21:14:55 GMT</pubDate></item><item><title><![CDATA[Reply to Absturz bei std::vector-&amp;gt;erase() on Mon, 30 Apr 2012 11:43:08 GMT]]></title><description><![CDATA[<p>Ich push das nochmal hoch, weil ich immer noch keine Lösung habe.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2206908</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2206908</guid><dc:creator><![CDATA[Dengar]]></dc:creator><pubDate>Mon, 30 Apr 2012 11:43:08 GMT</pubDate></item><item><title><![CDATA[Reply to Absturz bei std::vector-&amp;gt;erase() on Mon, 30 Apr 2012 11:53:07 GMT]]></title><description><![CDATA[<p><a href="http://www.c-plusplus.net/forum/200753" rel="nofollow">Reduziere Codebeispiele auf das Wesentliche</a></p>
<p>Oder hilf uns deinen Algorithmus zu verstehen. Das kannst du unter anderem auch dadurch erreichen, dass du deine Funktion <code>Atlas::getRegion</code> in kleinere Funktionen aufteilst, deren Namen eine Aussage darüber machen, was sie tun. Damit kann man dann jeden einzelnen Schritt im Algorithmus besser nachvollziehen und somit verstehen, was eigentlich gemacht wird.</p>
<p>Zudem ist es immer praktisch, wenn du ein Beispiel präsentierst, welches kompilierbar ist und den Fehler aufweist. Damit wir allenfalls selber den Debugger anwerfen können.</p>
<p>Grüssli</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2206913</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2206913</guid><dc:creator><![CDATA[Dravere]]></dc:creator><pubDate>Mon, 30 Apr 2012 11:53:07 GMT</pubDate></item><item><title><![CDATA[Reply to Absturz bei std::vector-&amp;gt;erase() on Mon, 30 Apr 2012 12:45:34 GMT]]></title><description><![CDATA[<p>Du weiß schon, dass die insert und erase Methoden von vector unter ( meisten ) Umständen alle deine vorher gespeicherten Iteratoren ungültig machen... oder???</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2206940</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2206940</guid><dc:creator><![CDATA[Travor]]></dc:creator><pubDate>Mon, 30 Apr 2012 12:45:34 GMT</pubDate></item></channel></rss>