<?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[static variable in Funktion]]></title><description><![CDATA[<p>Wen ich eine statische variable in einer Funktion deklariere:</p>
<pre><code>// deklariert in einer header datei
int getValue()
{
   static int type;
   return type++;
}
</code></pre>
<p>Diese funktion rufe ich jetzt aus zwei Dlls auf, dann habe ich in beiden DLLs<br />
zweimal</p>
<pre><code>type
</code></pre>
<p>initialisiert.<br />
Warum ist das so und wo ist das definiert, dass das so ist?<br />
Bei msdn habe ich nix gefunden.</p>
<p>Ist das verhalten bei g++ genauso?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/319083/static-variable-in-funktion</link><generator>RSS for Node</generator><lastBuildDate>Sat, 25 Jul 2026 10:10:35 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/319083.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 06 Aug 2013 09:39:06 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to static variable in Funktion on Tue, 06 Aug 2013 09:39:06 GMT]]></title><description><![CDATA[<p>Wen ich eine statische variable in einer Funktion deklariere:</p>
<pre><code>// deklariert in einer header datei
int getValue()
{
   static int type;
   return type++;
}
</code></pre>
<p>Diese funktion rufe ich jetzt aus zwei Dlls auf, dann habe ich in beiden DLLs<br />
zweimal</p>
<pre><code>type
</code></pre>
<p>initialisiert.<br />
Warum ist das so und wo ist das definiert, dass das so ist?<br />
Bei msdn habe ich nix gefunden.</p>
<p>Ist das verhalten bei g++ genauso?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2343827</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2343827</guid><dc:creator><![CDATA[Nash26]]></dc:creator><pubDate>Tue, 06 Aug 2013 09:39:06 GMT</pubDate></item><item><title><![CDATA[Reply to static variable in Funktion on Tue, 06 Aug 2013 10:14:45 GMT]]></title><description><![CDATA[<p>Du solltest in der Headerdatei deklarieren und nicht definieren. Du benutzt Header falsch!</p>
<blockquote>
<p>Warum ist das so</p>
</blockquote>
<p>Weil static sich nur auf die aktuelle Uebersetzungseinheit bezieht, innerhalb von Funktionen eben nur auf die aktuelle Funktion. Wenn du den Header mehrfach inkludierst, dann steht die Funktionsdefinition in mehreren Uebersetzungseinheiten. Deswegen hast du auch mehrere &quot;type&quot;. Der Linker kann das manchmal aufloesen, aber da du mehrere DLLs hast, hast du auch mehrere Linkeinheiten.</p>
<p>Du solltest weiterhin die Variable mit einem Wert initialisieren.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2343829</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2343829</guid><dc:creator><![CDATA[knivil]]></dc:creator><pubDate>Tue, 06 Aug 2013 10:14:45 GMT</pubDate></item><item><title><![CDATA[Reply to static variable in Funktion on Tue, 06 Aug 2013 10:01:03 GMT]]></title><description><![CDATA[<p>das ist ein bekanntes Problem bei template und inline Funktionen. bei nicht-inline Funktionen dürfte es eigentlich nicht auftreten</p>
<p><a href="http://www.google.de/webhp?hl=de&amp;tab=ww#bav=on.2,or.r_qf.&amp;hl=de&amp;q=c%2B%2B+template+static+member+dll" rel="nofollow">http://www.google.de/webhp?hl=de&amp;tab=ww#bav=on.2,or.r_qf.&amp;hl=de&amp;q=c%2B%2B+template+static+member+dll</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2343834</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2343834</guid><dc:creator><![CDATA[dd++ 0]]></dc:creator><pubDate>Tue, 06 Aug 2013 10:01:03 GMT</pubDate></item><item><title><![CDATA[Reply to static variable in Funktion on Tue, 06 Aug 2013 10:20:25 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/657">@dd</a>++<br />
Das ist nicht wirklich inline-spezifisch, sondern hat einfach damit zu tun wie DLLs funktionieren.<br />
Wenn die Funktion in einer statischen LIB definiert ist, die in zwei DLLs verwendet wird, dann gibt es genau so zwei getrennte Variablen.</p>
<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/22803">@Nash26</a><br />
Wenn mit mit &quot;g++&quot; z.B. MinGW meinst, also einen GCC Port der Windows DLLs erzeugen kann, dann ist das Verhalten dort vermutlich gleich. Weil DLLs die &quot;select any&quot; Semantik nicht unterstützen die nötig wäre um es anders zu machen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2343842</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2343842</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Tue, 06 Aug 2013 10:20:25 GMT</pubDate></item><item><title><![CDATA[Reply to static variable in Funktion on Tue, 06 Aug 2013 10:48:53 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/13960">@hustbaer</a><br />
ok ist dieses verhalten also nur windows spezifisch.</p>
<p>Ich hab schong gesucht aber nicht etwas wirklich passendes dazu auf msdn gefunden,<br />
ausser das hier:<br />
<a href="http://msdn.microsoft.com/de-de/library/h90dkhs0(v=vs.90).aspx" rel="nofollow">http://msdn.microsoft.com/de-de/library/h90dkhs0(v=vs.90).aspx</a></p>
<p>Hier geht es aber eher um eine DLL mit mehreren prozessen.</p>
<p>Hast du noch was handfestes do ich dieses Verhalten nachlesen kann?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2343850</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2343850</guid><dc:creator><![CDATA[Nash26]]></dc:creator><pubDate>Tue, 06 Aug 2013 10:48:53 GMT</pubDate></item><item><title><![CDATA[Reply to static variable in Funktion on Tue, 06 Aug 2013 11:06:33 GMT]]></title><description><![CDATA[<p>Nash26 schrieb:</p>
<blockquote>
<p>[...] Dlls [...]<br />
Warum ist das so [...]?<br />
Hier geht es aber eher um eine DLL mit mehreren prozessen.</p>
</blockquote>
<p>C++ kennt keine DLLs. Deine Frage ist System- bzw. Toolchain-spezifisch. Und wie sich verschiedene Prozesse Daten teilen können erst recht. Jeder Prozess bekommt seinen eigenen Speicher, inklusive eigene Variablen mit statischer Lebenszeit. Sinn von DLLs ist es, Code und Read-only-Daten nur einmal im Speicher zu halten. Das schließt aber nicht die änderbaren Datensegmente mit ein. Von denen gibt's für jeden Prozess eine eigene Version.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2343854</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2343854</guid><dc:creator><![CDATA[krümelkacker]]></dc:creator><pubDate>Tue, 06 Aug 2013 11:06:33 GMT</pubDate></item><item><title><![CDATA[Reply to static variable in Funktion on Tue, 06 Aug 2013 11:45:34 GMT]]></title><description><![CDATA[<p>etwas handfestes zum Nachlesen gibts z.B. hier:</p>
<p><a href="http://stackoverflow.com/questions/11962918/local-static-variable-is-instantiated-multiple-times-why" rel="nofollow">http://stackoverflow.com/questions/11962918/local-static-variable-is-instantiated-multiple-times-why</a></p>
<p>das ist vom Prinzip her ähnlich wie das weiter oben mit den templates</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2343867</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2343867</guid><dc:creator><![CDATA[dd++ 0]]></dc:creator><pubDate>Tue, 06 Aug 2013 11:45:34 GMT</pubDate></item><item><title><![CDATA[Reply to static variable in Funktion on Tue, 06 Aug 2013 21:01:39 GMT]]></title><description><![CDATA[<p>Achja, laut dahier:<br />
<a href="http://msdn.microsoft.com/en-us/library/w8c3skw3.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/w8c3skw3.aspx</a><br />
sollte <code>__declspec(dllexport)/__declspec(dllimport)</code> in diesem Fall ausreichend sein. Trotz <code>inline</code> und so.<br />
Wobei das MSVC-Magick ist und eben auch nur mit <code>dllexport/dllimport</code> funktioniert.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2344062</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2344062</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Tue, 06 Aug 2013 21:01:39 GMT</pubDate></item><item><title><![CDATA[Reply to static variable in Funktion on Sat, 17 Aug 2013 12:33:17 GMT]]></title><description><![CDATA[<p>Hab hier noch folgenden Link gefunden:<br />
<a href="http://msdn.microsoft.com/en-us/library/h90dkhs0%28v=vs.80%29.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/h90dkhs0%28v=vs.80%29.aspx</a></p>
<blockquote>
<p>Win32 DLLs are mapped into the address space of the calling process. By default, each process using a DLL has its own instance of all the DLLs global and static variables.</p>
</blockquote>
<p>Die sagen zwar das jeder Prozess eine Kopie lokaler und globaler statischen Daten bekommt, aber nicht was mit DLLs passiert die eine andere DLL benutzen.</p>
<p>Eine DLL ist doch nicht auch so etwas wie ein Prozess?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2346624</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2346624</guid><dc:creator><![CDATA[Nash26]]></dc:creator><pubDate>Sat, 17 Aug 2013 12:33:17 GMT</pubDate></item><item><title><![CDATA[Reply to static variable in Funktion on Mon, 19 Aug 2013 20:52:23 GMT]]></title><description><![CDATA[<p>Wie verhält es sich bei DLLs die andere DLLs anlinken?<br />
Gilt da die selbe Aussage wie bei dem Prozess?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2347107</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2347107</guid><dc:creator><![CDATA[Nash26]]></dc:creator><pubDate>Mon, 19 Aug 2013 20:52:23 GMT</pubDate></item><item><title><![CDATA[Reply to static variable in Funktion on Wed, 21 Aug 2013 10:03:25 GMT]]></title><description><![CDATA[<p>Nash26 schrieb:</p>
<blockquote>
<p>Die sagen zwar das jeder Prozess eine Kopie lokaler und globaler statischen Daten bekommt, aber nicht was mit DLLs passiert die eine andere DLL benutzen.</p>
</blockquote>
<p>Was meinst du, gäbe es denn noch für Optionen, was mit anderen DLLs passieren könnte, wenn doch schon klar ist, dass jeder Prozess hat seine eigenen Daten hat?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2347437</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2347437</guid><dc:creator><![CDATA[krümelkacker]]></dc:creator><pubDate>Wed, 21 Aug 2013 10:03:25 GMT</pubDate></item></channel></rss>