<?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[[Kein weiteres Interesse] lokale Variable: static vs. static const]]></title><description><![CDATA[<p>Hi,</p>
<p>habe hier gerade ein seltsames Problem, vielleicht kann mir ja jemand weiterhelfen.</p>
<pre><code class="language-cpp">void Form::update()
{
   static const time_t Offset = DateTimeToUnix( TDateTime::CurrentDateTime() ) - DateTimeToUnix( parse_date_time_string( &quot;2012-05-29 11:00:00&quot; ) );
}
</code></pre>
<p>Diese Funktion kehrt nach dem zweiten Aufruf nicht mehr zurück und die Anwendung bleibt stehen. Wenn ich jedoch das <strong>const</strong> entferne treten keine Probleme mehr auf. Ist das durch den Standard so gewollt (bzw. UB), oder ist das eine Compiler Macke?</p>
<p>Edit:<br />
Scheint irgendwie am Literal zu liegen, folgender Code ist in Ordnung und macht keine Probleme mehr:</p>
<pre><code class="language-cpp">void Form::update()
{
   static const AnsiString StartDate = &quot;2012-05-29 11:00:00&quot;;
   static const time_t Offset    = DateTimeToUnix( TDateTime::CurrentDateTime() ) - DateTimeToUnix( parse_date_time_string( StartDate ) );
</code></pre>
<p>Komisch <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/topic/304172/kein-weiteres-interesse-lokale-variable-static-vs-static-const</link><generator>RSS for Node</generator><lastBuildDate>Mon, 10 Aug 2026 02:54:37 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/304172.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 31 May 2012 09:22:24 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to [Kein weiteres Interesse] lokale Variable: static vs. static const on Thu, 31 May 2012 11:03:10 GMT]]></title><description><![CDATA[<p>Hi,</p>
<p>habe hier gerade ein seltsames Problem, vielleicht kann mir ja jemand weiterhelfen.</p>
<pre><code class="language-cpp">void Form::update()
{
   static const time_t Offset = DateTimeToUnix( TDateTime::CurrentDateTime() ) - DateTimeToUnix( parse_date_time_string( &quot;2012-05-29 11:00:00&quot; ) );
}
</code></pre>
<p>Diese Funktion kehrt nach dem zweiten Aufruf nicht mehr zurück und die Anwendung bleibt stehen. Wenn ich jedoch das <strong>const</strong> entferne treten keine Probleme mehr auf. Ist das durch den Standard so gewollt (bzw. UB), oder ist das eine Compiler Macke?</p>
<p>Edit:<br />
Scheint irgendwie am Literal zu liegen, folgender Code ist in Ordnung und macht keine Probleme mehr:</p>
<pre><code class="language-cpp">void Form::update()
{
   static const AnsiString StartDate = &quot;2012-05-29 11:00:00&quot;;
   static const time_t Offset    = DateTimeToUnix( TDateTime::CurrentDateTime() ) - DateTimeToUnix( parse_date_time_string( StartDate ) );
</code></pre>
<p>Komisch <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2217622</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2217622</guid><dc:creator><![CDATA[DocShoe]]></dc:creator><pubDate>Thu, 31 May 2012 11:03:10 GMT</pubDate></item><item><title><![CDATA[Reply to [Kein weiteres Interesse] lokale Variable: static vs. static const on Thu, 31 May 2012 09:36:36 GMT]]></title><description><![CDATA[<blockquote>
<p>nach dem zweiten Aufruf nicht mehr zurück</p>
</blockquote>
<p>Und das hast du wie festgestellt? Beim ersten Aufruf wird die statische Variable initialisiert. Sie ist <code>const</code> , ein zweiter Aufruf gleicht einer leeren Methode.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2217625</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2217625</guid><dc:creator><![CDATA[knivil]]></dc:creator><pubDate>Thu, 31 May 2012 09:36:36 GMT</pubDate></item><item><title><![CDATA[Reply to [Kein weiteres Interesse] lokale Variable: static vs. static const on Thu, 31 May 2012 09:52:23 GMT]]></title><description><![CDATA[<p>DocShoe schrieb:</p>
<blockquote>
<p>Hi,</p>
<p>habe hier gerade ein seltsames Problem, vielleicht kann mir ja jemand weiterhelfen.</p>
<pre><code class="language-cpp">void Form::update()
{
   static const time_t Offset = DateTimeToUnix( TDateTime::CurrentDateTime() ) - DateTimeToUnix( parse_date_time_string( &quot;2012-05-29 11:00:00&quot; ) );
}
</code></pre>
<p>Diese Funktion kehrt nach dem zweiten Aufruf nicht mehr zurück und die Anwendung bleibt stehen. Wenn ich jedoch das <strong>const</strong> entferne treten keine Probleme mehr auf. Ist das durch den Standard so gewollt (bzw. UB), oder ist das eine Compiler Macke?</p>
</blockquote>
<p>So wie es aussieht hast du entweder nicht den echten Code gepostet, <strong>oder das Abstürzen hat nix mit der Methode zu tun</strong> sondern ist ein Sideeffect (vllt. von DateTimeUnix oder so). Eine statische Variable (dazu const) wird einmal initialisiert. Das wars dann.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2217638</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2217638</guid><dc:creator><![CDATA[Sone]]></dc:creator><pubDate>Thu, 31 May 2012 09:52:23 GMT</pubDate></item><item><title><![CDATA[Reply to [Kein weiteres Interesse] lokale Variable: static vs. static const on Thu, 31 May 2012 09:54:19 GMT]]></title><description><![CDATA[<p>Einfach mit dem Debugger durchgegangen. Nach Verlassen des Funktionsrumpfes scheppert´s im Destruktor eines AnsiStrings. Der wurde temporär für den Aufruf <code>parse_date_time_string( const AnsiString&amp; DateTime )</code> erzeugt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2217641</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2217641</guid><dc:creator><![CDATA[DocShoe]]></dc:creator><pubDate>Thu, 31 May 2012 09:54:19 GMT</pubDate></item><item><title><![CDATA[Reply to [Kein weiteres Interesse] lokale Variable: static vs. static const on Thu, 31 May 2012 09:56:08 GMT]]></title><description><![CDATA[<p>Hacker schrieb:</p>
<blockquote>
<p>So wie es aussieht hast du entweder nicht den echten Code gepostet, <strong>oder das Abstürzen hat nix mit der Methode zu tun</strong> sondern ist ein Sideeffect (vllt. von DateTimeUnix oder so). Eine statische Variable (dazu const) wird einmal initialisiert. Das wars dann.</p>
</blockquote>
<p>Bin jetzt lang genug dabei, um solche Sachen auszuschließen. Der Code ist minimal und reproduziert den Fehler zu 100%.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2217642</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2217642</guid><dc:creator><![CDATA[DocShoe]]></dc:creator><pubDate>Thu, 31 May 2012 09:56:08 GMT</pubDate></item><item><title><![CDATA[Reply to [Kein weiteres Interesse] lokale Variable: static vs. static const on Thu, 31 May 2012 09:58:36 GMT]]></title><description><![CDATA[<p>DocShoe schrieb:</p>
<blockquote>
<p>scheppert´s im Destruktor eines AnsiStrings.</p>
</blockquote>
<p>Was scheppert? Speicherfreigabe? Du siehst doch die Anweisung, nehme ich an?<br />
Kannst du andere Strings normal benutzen?<br />
Beim zweiten Funktionsaufruf passiert rein gar nichts.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2217643</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2217643</guid><dc:creator><![CDATA[Sone]]></dc:creator><pubDate>Thu, 31 May 2012 09:58:36 GMT</pubDate></item><item><title><![CDATA[Reply to [Kein weiteres Interesse] lokale Variable: static vs. static const on Thu, 31 May 2012 10:04:21 GMT]]></title><description><![CDATA[<p>Na hast du vermutlich Bug in parse_date_time_string() und/oder der AnsiString Klasse.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2217650</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2217650</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Thu, 31 May 2012 10:04:21 GMT</pubDate></item><item><title><![CDATA[Reply to [Kein weiteres Interesse] lokale Variable: static vs. static const on Thu, 31 May 2012 10:05:22 GMT]]></title><description><![CDATA[<p>Ich vermute mal dein Problem liegt in parse_date_time_string(). Vermutlich schreibst du dort über Arraygrenzen? Das erklärt wieso es im Destruktor des AnsiString scheppert (der Debug Heap merkt beim delete[] dass was kaputtgegangen ist). Und es erklärt auch wieso es mit dem Stringliteral undefiniertes Verhalten liefert...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2217651</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2217651</guid><dc:creator><![CDATA[dot]]></dc:creator><pubDate>Thu, 31 May 2012 10:05:22 GMT</pubDate></item><item><title><![CDATA[Reply to [Kein weiteres Interesse] lokale Variable: static vs. static const on Thu, 31 May 2012 10:09:13 GMT]]></title><description><![CDATA[<p>DocShoe schrieb:</p>
<blockquote>
<p>Der Code ist minimal und reproduziert den Fehler zu 100%.</p>
</blockquote>
<p>Der Code ist weniger als minimal. Ich krieg da</p>
<pre><code>prog.cpp:1: error: ‘Form’ has not been declared
prog.cpp: In function ‘void update()’:
prog.cpp:3: error: ‘time_t’ does not name a type
</code></pre>
<p>Also Pustekuchen mit Fehler reproduzieren.</p>
<p>Liefer uns den vollständigen Code, dann können wir uns mit befassen. Siehe zweiter Link in meiner Signatur unten.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2217655</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2217655</guid><dc:creator><![CDATA[pumuckl]]></dc:creator><pubDate>Thu, 31 May 2012 10:09:13 GMT</pubDate></item><item><title><![CDATA[Reply to [Kein weiteres Interesse] lokale Variable: static vs. static const on Thu, 31 May 2012 10:17:04 GMT]]></title><description><![CDATA[<p>Nein, <code>AnsiString</code> und <code>parse_date_time</code> funktionieren. Hab´ allerdings noch einmal ein paar Zeilen Code hin- und hergeschoben:</p>
<pre><code class="language-cpp">time_t func()
{
   return DateTimeToUnix( parse_date_time_string( &quot;2012-05-29 11:00:00&quot; ) );
}

void Form::update()
{
   static time_t t1 = DateTimeToUnix( parse_date_time_string( &quot;2012-05-29 11:00:00&quot; ) ); // geht
   static const time_t t2 = DateTimeToUnix( parse_date_time_string( &quot;2012-05-29 11:00:00&quot; ) ); // geht nicht
   static time_t t3 = func(); // geht
   static const time_t t4 = func(); // geht
}
</code></pre>
<p>Der Bug ist zwar gefixt, aber mich interessiert trotzdem, warum Variante 2 nicht funktioniert. Wenn <code>AnsiString</code> oder <code>parse_date_time</code> buggy wären dürften alle anderen Varianten auch nicht funktionieren (gut, bei UB schwer festzustellen), aber t1 - t4 sind identisch und weisen auf korrekte Funktionsweise hin. Ich hake das als RAD Studio Macke ab, den Fix habe ich ja.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2217660</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2217660</guid><dc:creator><![CDATA[DocShoe]]></dc:creator><pubDate>Thu, 31 May 2012 10:17:04 GMT</pubDate></item><item><title><![CDATA[Reply to [Kein weiteres Interesse] lokale Variable: static vs. static const on Thu, 31 May 2012 10:25:16 GMT]]></title><description><![CDATA[<p>Ach komm, mit über 1500 Beiträgen solltest du doch mal so langsam mitbekommen haben, dass wir den Fehler nachvollziehen können müssen, wenn wir dir bei solchen Problemen helfen sollen. Also <strong>vollständiges</strong> Minimalbeispiel, keine Behauptungen über die angebliche Fehlerfreiheit des restlichen Codes, <strong>besonders</strong> nicht, wenn man geheimnisvolle Fehler hat. pumuckl hat gestern dazu so einen schönen Beitrag geschrieben, du findest ihn als dritten Link in meiner Signatur.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2217661</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2217661</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Thu, 31 May 2012 10:25:16 GMT</pubDate></item><item><title><![CDATA[Reply to [Kein weiteres Interesse] lokale Variable: static vs. static const on Thu, 31 May 2012 10:26:15 GMT]]></title><description><![CDATA[<p>SeppJ schrieb:</p>
<blockquote>
<p>Ach komm, mit über 1500 Beiträgen solltest du doch mal so langsam mitbekommen haben, dass wir den Fehler nachvollziehen können müssen, wenn wir dir bei solchen Problemen helfen sollen. Also <strong>vollständiges</strong> Minimalbeispiel, keine Behauptungen über die angebliche Fehlerfreiheit des restlichen Codes, <strong>besonders</strong> nicht, wenn man geheimnisvolle Fehler hat. pumuckl hat gestern dazu so einen schönen Beitrag geschrieben, du findest ihn als dritten Link in meiner Signatur.</p>
</blockquote>
<p>Aber sein Code ist doch Plattformabhängig? Dann bringt das sowieso nichts wenn 60% der User hier einen Windows-Pc oder einen Mac haben <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2217662</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2217662</guid><dc:creator><![CDATA[Sone]]></dc:creator><pubDate>Thu, 31 May 2012 10:26:15 GMT</pubDate></item><item><title><![CDATA[Reply to [Kein weiteres Interesse] lokale Variable: static vs. static const on Thu, 31 May 2012 10:49:04 GMT]]></title><description><![CDATA[<p>DocShoe schrieb:</p>
<blockquote>
<pre><code class="language-cpp">time_t func()
{
   return DateTimeToUnix( parse_date_time_string( &quot;2012-05-29 11:00:00&quot; ) );
}

void Form::update()
{
   static time_t t1 = DateTimeToUnix( parse_date_time_string( &quot;2012-05-29 11:00:00&quot; ) ); // geht
   static const time_t t2 = DateTimeToUnix( parse_date_time_string( &quot;2012-05-29 11:00:00&quot; ) ); // geht nicht
   static time_t t3 = func(); // geht
   static const time_t t4 = func(); // geht
}
</code></pre>
</blockquote>
<p>Nach Adam Riese liegt der Fehler in DateTimeToUnix oder parse_date_time_string.</p>
<p>Das sollte eigentlich offensichtlich sein... Irgendwo entsteht UB.</p>
<p>@Hacker:<br />
Ja, n bisschen Grips ist notwendig um ein Minimalbeispiel zu bauen dass funktioniert. String Klassen kann man idR ohne Probleme durch std::string ersetzen und Form ist unnötig. Puh, das kostet mich vielleicht 40 Sekunden arbeit. Oh Mein Gott.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2217666</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2217666</guid><dc:creator><![CDATA[Shade Of Mine]]></dc:creator><pubDate>Thu, 31 May 2012 10:49:04 GMT</pubDate></item><item><title><![CDATA[Reply to [Kein weiteres Interesse] lokale Variable: static vs. static const on Thu, 31 May 2012 10:49:45 GMT]]></title><description><![CDATA[<p>Schepperts hier auch?</p>
<pre><code class="language-cpp">static const XYZ t2 = parse_date_time_string( &quot;2012-05-29 11:00:00&quot; );
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2217669</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2217669</guid><dc:creator><![CDATA[out]]></dc:creator><pubDate>Thu, 31 May 2012 10:49:45 GMT</pubDate></item><item><title><![CDATA[Reply to [Kein weiteres Interesse] lokale Variable: static vs. static const on Thu, 31 May 2012 11:00:29 GMT]]></title><description><![CDATA[<p>Edit: Doppelpost</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2217675</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2217675</guid><dc:creator><![CDATA[Sone]]></dc:creator><pubDate>Thu, 31 May 2012 11:00:29 GMT</pubDate></item><item><title><![CDATA[Reply to [Kein weiteres Interesse] lokale Variable: static vs. static const on Thu, 31 May 2012 11:00:16 GMT]]></title><description><![CDATA[<p>Shade Of Mine schrieb:</p>
<blockquote>
<p>@Hacker:<br />
Ja, n bisschen Grips ist notwendig um ein Minimalbeispiel zu bauen dass funktioniert. String Klassen kann man idR ohne Probleme durch std::string ersetzen und Form ist unnötig. Puh, das kostet mich vielleicht 40 Sekunden arbeit. Oh Mein Gott.</p>
</blockquote>
<p>Jau. Und das kann den Fehler aber auch schon lösen <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="😉"
    /> denn vielleicht ist er ja gerade dann in den Plattformabhängigen Libs versteckt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2217676</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2217676</guid><dc:creator><![CDATA[Sone]]></dc:creator><pubDate>Thu, 31 May 2012 11:00:16 GMT</pubDate></item><item><title><![CDATA[Reply to [Kein weiteres Interesse] lokale Variable: static vs. static const on Thu, 31 May 2012 12:30:44 GMT]]></title><description><![CDATA[<p>Hacker schrieb:</p>
<blockquote>
<p>Jau. Und das kann den Fehler aber auch schon lösen <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="😉"
    /> denn vielleicht ist er ja gerade dann in den Plattformabhängigen Libs versteckt.</p>
</blockquote>
<p>Dann haben wir den Fehler gefunden und das Problem gelöst. Perfekto.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2217722</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2217722</guid><dc:creator><![CDATA[Shade Of Mine]]></dc:creator><pubDate>Thu, 31 May 2012 12:30:44 GMT</pubDate></item></channel></rss>