<?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[Funktion &amp;quot;exit&amp;quot; und Aufräumen des Stacks]]></title><description><![CDATA[<p>Wie kann ich es beim Aufrufen von <code>exit</code> hinbekommen, dass der Stack vorher noch aufgeräumt wird?</p>
<p>Beispiel: Ich habe folgenden Quellcode:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;cstdlib&gt;

using namespace std;

class Test
{
public:

	~Test()
	{
		cout &lt;&lt; &quot;Destruktor\n&quot;;
	}
};

void Ende()
{
	cout &lt;&lt; &quot;Ende-Funktion\n&quot;;
}

int main()
{
	atexit(Ende);

	Test t;

	// exit(0);

	cout &lt;&lt; &quot;Ende main\n&quot;;
}
</code></pre>
<p>Dieser gibt mir folgendes aus:</p>
<pre><code>Ende main
Destruktor
Ende-Funktion
</code></pre>
<p>So weit, so gut. Wenn ich nun aber den auskommentierten Aufruf in Zeile 27 wieder reinnehme, gibt es nur noch folgendes als Ausgabe:</p>
<pre><code>Ende-Funktion
</code></pre>
<p>Ich hätte jetzt aber erwartet, dass er den Destruktor der Klasse <code>Test</code> noch aufruft und somit den Stack bereinigt, statt die Variablen jetzt unaufgeräumt stehen zu lassen. Würde ich <code>exit(0);</code> durch <code>return 0;</code> ersetzen, würde ja dort auch</p>
<pre><code>Destruktor
Ende-Funktion
</code></pre>
<p>stehen.</p>
<p>Deshalb würde ich gerne wissen:<br />
1. Wieso wird die lokale Variable nicht korrekt zerstört? Im Gegensatz zur <code>abort</code> -Funktion heißt es über <code>exit</code> ja schließlich:</p>
<p><a href="http://www.cplusplus.com/reference/clibrary/cstdlib/exit" rel="nofollow">www.cplusplus.com/reference/clibrary/cstdlib/exit</a> schrieb:</p>
<blockquote>
<p>Terminates the process normally, performing the regular cleanup for terminating processes.</p>
</blockquote>
<p>2. Wie kann ich sicherstellen, dass meine auf dem Stack erstellten Variablen im Falle eines Aufrufs von <code>exit</code> sauber gelöscht werden?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/284897/funktion-quot-exit-quot-und-aufräumen-des-stacks</link><generator>RSS for Node</generator><lastBuildDate>Fri, 21 Aug 2026 18:20:15 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/284897.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 10 Apr 2011 18:27:11 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Funktion &amp;quot;exit&amp;quot; und Aufräumen des Stacks on Sun, 10 Apr 2011 18:27:11 GMT]]></title><description><![CDATA[<p>Wie kann ich es beim Aufrufen von <code>exit</code> hinbekommen, dass der Stack vorher noch aufgeräumt wird?</p>
<p>Beispiel: Ich habe folgenden Quellcode:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;cstdlib&gt;

using namespace std;

class Test
{
public:

	~Test()
	{
		cout &lt;&lt; &quot;Destruktor\n&quot;;
	}
};

void Ende()
{
	cout &lt;&lt; &quot;Ende-Funktion\n&quot;;
}

int main()
{
	atexit(Ende);

	Test t;

	// exit(0);

	cout &lt;&lt; &quot;Ende main\n&quot;;
}
</code></pre>
<p>Dieser gibt mir folgendes aus:</p>
<pre><code>Ende main
Destruktor
Ende-Funktion
</code></pre>
<p>So weit, so gut. Wenn ich nun aber den auskommentierten Aufruf in Zeile 27 wieder reinnehme, gibt es nur noch folgendes als Ausgabe:</p>
<pre><code>Ende-Funktion
</code></pre>
<p>Ich hätte jetzt aber erwartet, dass er den Destruktor der Klasse <code>Test</code> noch aufruft und somit den Stack bereinigt, statt die Variablen jetzt unaufgeräumt stehen zu lassen. Würde ich <code>exit(0);</code> durch <code>return 0;</code> ersetzen, würde ja dort auch</p>
<pre><code>Destruktor
Ende-Funktion
</code></pre>
<p>stehen.</p>
<p>Deshalb würde ich gerne wissen:<br />
1. Wieso wird die lokale Variable nicht korrekt zerstört? Im Gegensatz zur <code>abort</code> -Funktion heißt es über <code>exit</code> ja schließlich:</p>
<p><a href="http://www.cplusplus.com/reference/clibrary/cstdlib/exit" rel="nofollow">www.cplusplus.com/reference/clibrary/cstdlib/exit</a> schrieb:</p>
<blockquote>
<p>Terminates the process normally, performing the regular cleanup for terminating processes.</p>
</blockquote>
<p>2. Wie kann ich sicherstellen, dass meine auf dem Stack erstellten Variablen im Falle eines Aufrufs von <code>exit</code> sauber gelöscht werden?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2047229</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2047229</guid><dc:creator><![CDATA[Hm-Bob]]></dc:creator><pubDate>Sun, 10 Apr 2011 18:27:11 GMT</pubDate></item><item><title><![CDATA[Reply to Funktion &amp;quot;exit&amp;quot; und Aufräumen des Stacks on Sun, 10 Apr 2011 18:40:00 GMT]]></title><description><![CDATA[<p>Die Funktion exit stammt noch aus C und dort gibt es bekanntlich keine Klassen mit Konstruktoren/Destruktoren. Folglich ist die Funktion in C++ nicht zu empfehlen. Für Fehlerbehandlung, etc. gibt es in C++ Exceptions.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2047235</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2047235</guid><dc:creator><![CDATA[klöklö]]></dc:creator><pubDate>Sun, 10 Apr 2011 18:40:00 GMT</pubDate></item><item><title><![CDATA[Reply to Funktion &amp;quot;exit&amp;quot; und Aufräumen des Stacks on Sun, 10 Apr 2011 18:40:25 GMT]]></title><description><![CDATA[<p>Steht so im Standard. In einer Fußnote steht sogar eine mögliche Lösung für dein Problem:</p>
<p>ISO/IEC 14882:2003 18.3 (8) schrieb:</p>
<blockquote>
<p>The function exit() has additional behavior in this International Standard:</p>
<p>— First, objects with static storage duration are destroyed and functions registered by calling atexit are called. Non-local objects with static storage duration are destroyed in the reverse order of the completion of their constructor. <strong>(Automatic objects are not destroyed as a result of calling exit().)</strong><sup>207)</sup> Functions registered with atexit are called in the reverse order of their registration, except that a function is called after any previously registered functions that had already been called at the time it was registered.<sup>208)</sup> A function registered with atexit before a non-local object obj1 of static storage duration is initialized will not be called until obj1’s destruction has completed. A function registered with atexit after a non-local object obj2 of static storage duration is initialized will be called before obj2’s destruction starts. A local static object obj3 is destroyed at the same time it would be if a function calling the obj3 destructor were registered with atexit at the completion of the obj3 constructor.<br />
— Next, all open C streams (as mediated by the function signatures declared in &lt;cstdio&gt;) with unwritten buffered data are flushed, all open C streams are closed, and all files created by calling tmpfile() are removed.<sup>209)</sup><br />
— Finally, control is returned to the host environment. If status is zero or EXIT_SUCCESS, an implementation-defined form of the status successful termination is returned. If status is EXIT_FAILURE, an implementation-defined form of the status unsuccessful termination is returned. Otherwise the status returned is implementation-defined.<sup>210)</sup></p>
<p><strong><sup>207)</sup> Objects with automatic storage duration are all destroyed in a program whose function main() contains no automatic objects and executes the call to exit(). Control can be transferred directly to such a main() by throwing an exception that is caught in main().</strong><br />
<sup>208)</sup> A function is called for every time it is registered.<br />
<sup>209)</sup> Any C streams associated with cin, cout, etc (27.3) are flushed and closed when static objects are destroyed in the previous phase. The function tmpfile() is declared in &lt;cstdio&gt;.<br />
<sup>210)</sup> The macros EXIT_FAILURE and EXIT_SUCCESS are defined in &lt;cstdlib&gt;.</p>
</blockquote>
]]></description><link>https://www.c-plusplus.net/forum/post/2047237</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2047237</guid><dc:creator><![CDATA[seldon]]></dc:creator><pubDate>Sun, 10 Apr 2011 18:40:25 GMT</pubDate></item><item><title><![CDATA[Reply to Funktion &amp;quot;exit&amp;quot; und Aufräumen des Stacks on Sun, 10 Apr 2011 18:50:11 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;cstdlib&gt;

using namespace std;

class ExitException
{
private:
	size_t code;

public:
	ExitException(size_t code_) 
		: code(code_) 
	{
	}

	void Exit() const
	{ 
		exit(code); 
	}
};

class Test
{
public:

    ~Test()
    {
        cout &lt;&lt; &quot;Destruktor\n&quot;;
    }
}; 

void Ende()
{
    cout &lt;&lt; &quot;Ende-Funktion\n&quot;;
} 

int main()
{
	try
	{
		atexit(Ende); 

		Test t; 

		throw ExitException(0);

		cout &lt;&lt; &quot;Ende main\n&quot;; 
	}
	catch (ExitException&amp; e)
	{
		e.Exit();
	}
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2047240</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2047240</guid><dc:creator><![CDATA[klöklö]]></dc:creator><pubDate>Sun, 10 Apr 2011 18:50:11 GMT</pubDate></item></channel></rss>