<?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[[Gelöst] String::operator+= im Konstruktor liefert einen Segmentation Fault]]></title><description><![CDATA[<p>Morgen,</p>
<p>beim längst überfälligen Umstellen von <code>#define</code> nach <code>const</code> - ein Dank an Scott Mayers - stoße ich ein sich mir nicht erschließen wollendes Problem.</p>
<p>Es liegt folgender Quellcode vor:<br />
constats.h</p>
<pre><code class="language-cpp">// Include Guards, Includes, Konstanten, Enums
const std::string FILE_CONFIG       = &quot;config.xml&quot;;
// ...
</code></pre>
<p>Konstruktor in gameConfig.cpp</p>
<pre><code class="language-cpp">GameConfig::GameConfig()
    : path_(&quot;&quot;)
    // restliche Initializer List
{
    #ifdef UNIX
        path_ = getenv(&quot;HOME&quot;);
        path_ += &quot;/.Rostsommernacht/&quot;;
        std::string mkdir = &quot;if [ ! -d &quot;;
	    mkdir += path_;
	    mkdir += &quot; ]; then mkdir &quot;;
	    mkdir += path_;
	    mkdir += &quot;; fi&quot;;
        system(mkdir.c_str());
    #endif
    path_ += FILE_CONFIG; // Hier liegt laut Debugger gdb das Problem.

    load();
}
</code></pre>
<p>Ursprünglich war <code>FILE_CONFIG</code> durch <code>#define</code> definiert und auch, wenn ich es per Hand in <code>path_ += &quot;config.xml&quot;;</code> umändere, funktioniert alles so, wie es soll.</p>
<p>Doch mit dem derzeitigen Code bricht das Programm im Debugger mit folgender Fehlermeldung ab:</p>
<blockquote>
<p>Program received signal SIGSEGV, Segmentation fault.<br />
size (this=0x62dad0)<br />
at /usr/src/debug/gcc-4.6.2-20111027/obj-x86_64-redhat-linux/x86_64-redhat-linux/libstdc++-v3/include/bits/basic_string.h:711<br />
711 { return _M_rep()-&gt;_M_length; }</p>
</blockquote>
<p>Wie gesagt, kann ich mir dieses Verhalten nicht erklären und bitte um Hilfe.<br />
Als Compiler nutze ich <code>gcc-Version 4.6.2 20111027 (Red Hat 4.6.2-1) (GCC)</code> mit C++11.</p>
<p>Mit freundlichen Grüßen,<br />
Ki</p>
<p>EDIT:<br />
Der Codeteil im <code>#ifdef</code> -Block scheint das Problem übrigens nicht zu beeinflussen. Ein Auskommentieren sorgt jedenfalls <strong>nicht</strong> dafür, dass der Fehler nicht mehr auftritt.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/298050/gelöst-string-operator-im-konstruktor-liefert-einen-segmentation-fault</link><generator>RSS for Node</generator><lastBuildDate>Fri, 14 Aug 2026 03:14:38 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/298050.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 12 Jan 2012 15:19:13 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to [Gelöst] String::operator+= im Konstruktor liefert einen Segmentation Fault on Thu, 12 Jan 2012 16:51:48 GMT]]></title><description><![CDATA[<p>Morgen,</p>
<p>beim längst überfälligen Umstellen von <code>#define</code> nach <code>const</code> - ein Dank an Scott Mayers - stoße ich ein sich mir nicht erschließen wollendes Problem.</p>
<p>Es liegt folgender Quellcode vor:<br />
constats.h</p>
<pre><code class="language-cpp">// Include Guards, Includes, Konstanten, Enums
const std::string FILE_CONFIG       = &quot;config.xml&quot;;
// ...
</code></pre>
<p>Konstruktor in gameConfig.cpp</p>
<pre><code class="language-cpp">GameConfig::GameConfig()
    : path_(&quot;&quot;)
    // restliche Initializer List
{
    #ifdef UNIX
        path_ = getenv(&quot;HOME&quot;);
        path_ += &quot;/.Rostsommernacht/&quot;;
        std::string mkdir = &quot;if [ ! -d &quot;;
	    mkdir += path_;
	    mkdir += &quot; ]; then mkdir &quot;;
	    mkdir += path_;
	    mkdir += &quot;; fi&quot;;
        system(mkdir.c_str());
    #endif
    path_ += FILE_CONFIG; // Hier liegt laut Debugger gdb das Problem.

    load();
}
</code></pre>
<p>Ursprünglich war <code>FILE_CONFIG</code> durch <code>#define</code> definiert und auch, wenn ich es per Hand in <code>path_ += &quot;config.xml&quot;;</code> umändere, funktioniert alles so, wie es soll.</p>
<p>Doch mit dem derzeitigen Code bricht das Programm im Debugger mit folgender Fehlermeldung ab:</p>
<blockquote>
<p>Program received signal SIGSEGV, Segmentation fault.<br />
size (this=0x62dad0)<br />
at /usr/src/debug/gcc-4.6.2-20111027/obj-x86_64-redhat-linux/x86_64-redhat-linux/libstdc++-v3/include/bits/basic_string.h:711<br />
711 { return _M_rep()-&gt;_M_length; }</p>
</blockquote>
<p>Wie gesagt, kann ich mir dieses Verhalten nicht erklären und bitte um Hilfe.<br />
Als Compiler nutze ich <code>gcc-Version 4.6.2 20111027 (Red Hat 4.6.2-1) (GCC)</code> mit C++11.</p>
<p>Mit freundlichen Grüßen,<br />
Ki</p>
<p>EDIT:<br />
Der Codeteil im <code>#ifdef</code> -Block scheint das Problem übrigens nicht zu beeinflussen. Ein Auskommentieren sorgt jedenfalls <strong>nicht</strong> dafür, dass der Fehler nicht mehr auftritt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2167185</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2167185</guid><dc:creator><![CDATA[Ki]]></dc:creator><pubDate>Thu, 12 Jan 2012 16:51:48 GMT</pubDate></item><item><title><![CDATA[Reply to [Gelöst] String::operator+= im Konstruktor liefert einen Segmentation Fault on Thu, 12 Jan 2012 15:24:47 GMT]]></title><description><![CDATA[<p>Ist path_ ein std::string oder ein cstring?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2167191</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2167191</guid><dc:creator><![CDATA[pyhax]]></dc:creator><pubDate>Thu, 12 Jan 2012 15:24:47 GMT</pubDate></item><item><title><![CDATA[Reply to [Gelöst] String::operator+= im Konstruktor liefert einen Segmentation Fault on Thu, 12 Jan 2012 15:26:58 GMT]]></title><description><![CDATA[<p>path_ ist ein std::string</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2167197</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2167197</guid><dc:creator><![CDATA[Ki]]></dc:creator><pubDate>Thu, 12 Jan 2012 15:26:58 GMT</pubDate></item><item><title><![CDATA[Reply to [Gelöst] String::operator+= im Konstruktor liefert einen Segmentation Fault on Thu, 12 Jan 2012 15:31:01 GMT]]></title><description><![CDATA[<p>Was passiert, wenn du</p>
<pre><code class="language-cpp">const char FILE_CONFIG[]       = &quot;config.xml&quot;;
</code></pre>
<p>verwendest?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2167201</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2167201</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Thu, 12 Jan 2012 15:31:01 GMT</pubDate></item><item><title><![CDATA[Reply to [Gelöst] String::operator+= im Konstruktor liefert einen Segmentation Fault on Thu, 12 Jan 2012 15:32:38 GMT]]></title><description><![CDATA[<p>Ich hatte einen ähnlichen Fall, vieleicht gilt das auch für Dich.</p>
<p>Ich hatte zwei statische Objekte und der Konstruktor des einen hat die andere benutzt. Dummerweise wurde dessen Konstruktor noch gar nicht aufgerufen.</p>
<p>Was versprichst Du Dir davon, wenn Du &quot;config.xml&quot; in eine const std:string instanz reinschreibst? Ein const char * hätte es auch getan.</p>
<p>mfg Martin</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2167202</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2167202</guid><dc:creator><![CDATA[mgaeckler]]></dc:creator><pubDate>Thu, 12 Jan 2012 15:32:38 GMT</pubDate></item><item><title><![CDATA[Reply to [Gelöst] String::operator+= im Konstruktor liefert einen Segmentation Fault on Thu, 12 Jan 2012 15:38:10 GMT]]></title><description><![CDATA[<p>Ich war der Ansicht, ein std::string würde sich besser handhabe.</p>
<p>Wenn ich aus FILE_CONFIG als Char-Array deklariere, funktioniert alles so, wie es soll.</p>
<p>Das heißt wohl, es ist ratsamer und &quot;einfacher&quot;, bei konstanten Zeichenketten Char-Array zu nutzen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2167207</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2167207</guid><dc:creator><![CDATA[Ki]]></dc:creator><pubDate>Thu, 12 Jan 2012 15:38:10 GMT</pubDate></item><item><title><![CDATA[Reply to [Gelöst] String::operator+= im Konstruktor liefert einen Segmentation Fault on Thu, 12 Jan 2012 15:41:16 GMT]]></title><description><![CDATA[<p>Es wäre ratsam, dein GameConfig Objekt nicht statisch zu machen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2167209</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2167209</guid><dc:creator><![CDATA[cooky451]]></dc:creator><pubDate>Thu, 12 Jan 2012 15:41:16 GMT</pubDate></item><item><title><![CDATA[Reply to [Gelöst] String::operator+= im Konstruktor liefert einen Segmentation Fault on Thu, 12 Jan 2012 15:45:52 GMT]]></title><description><![CDATA[<p>Das GameConfig Objekt ist nicht statisch, aber global.</p>
<p>Jetzt bitte kein Shitstorm, ich weiß, dass wie geschätzte 99% der hiesigen Programmierer zu globalen Variablen stehen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2167212</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2167212</guid><dc:creator><![CDATA[Ki]]></dc:creator><pubDate>Thu, 12 Jan 2012 15:45:52 GMT</pubDate></item><item><title><![CDATA[Reply to [Gelöst] String::operator+= im Konstruktor liefert einen Segmentation Fault on Thu, 12 Jan 2012 16:38:02 GMT]]></title><description><![CDATA[<p>Das Problem ist einfach, dass die Initialisierungsreihenfolge globaler Objekte, die sich nicht in der selben Übersetzungseinheit befinden, unbestimmt ist. In Deinem Fall wird einfach GameConfig vor FILE_CONFIG konstruiert.</p>
<p>Unter anderem deshalb werden globale Variablen in C++ besonders verteufelt.</p>
<p>Lösung: Lass die Konstanten global, nutze aber für die Objekte ein Pattern wie das (Meyers-)Singleton.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2167240</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2167240</guid><dc:creator><![CDATA[LordJaxom]]></dc:creator><pubDate>Thu, 12 Jan 2012 16:38:02 GMT</pubDate></item><item><title><![CDATA[Reply to [Gelöst] String::operator+= im Konstruktor liefert einen Segmentation Fault on Thu, 12 Jan 2012 16:51:35 GMT]]></title><description><![CDATA[<p>Werde ich versuchen, notfalls nutze ich sonst die bereits vorgeschlagenen Char-Arrays.</p>
<p>Danke für die Hilfe <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>
]]></description><link>https://www.c-plusplus.net/forum/post/2167251</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2167251</guid><dc:creator><![CDATA[Ki]]></dc:creator><pubDate>Thu, 12 Jan 2012 16:51:35 GMT</pubDate></item><item><title><![CDATA[Reply to [Gelöst] String::operator+= im Konstruktor liefert einen Segmentation Fault on Fri, 13 Jan 2012 02:38:38 GMT]]></title><description><![CDATA[<p>Nein, GameConfig ist auch kein Singleton. Was machst du denn, wenn du mal noch eine Configdatei hast? Und Fehlerbehandlung ist bei Variablen mit statischer Lebenszeit auch ätzend. Also:</p>
<pre><code class="language-cpp">GameConfig::GameConfig(const std::string&amp; s)
{
  // ..
}

int main()
{
  try
  {
    GameConfig main_config(&quot;config.xml&quot;);
    // Game loop
  }
  catch ( /* */ )
  {
  }
}
</code></pre>
<p>Und wenn dann wirklich nahezu jede Funktion an dieses Objekt muss, kannst du immer noch einen globalen Pointer darauf initialisieren. Aber das würde ich nur im Notfall machen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2167462</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2167462</guid><dc:creator><![CDATA[cooky451]]></dc:creator><pubDate>Fri, 13 Jan 2012 02:38:38 GMT</pubDate></item></channel></rss>