<?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[Seltsames Verhalten bei Benutzung von op[] mit const char * als Schlüssel]]></title><description><![CDATA[<p>Hi,</p>
<p>ich habe mein Problem auf folgendes Stück Code reduzieren können:</p>
<pre><code class="language-cpp">#include &lt;unordered_map&gt;
#include &lt;string&gt;
#include &lt;cstdio&gt;

using namespace std;

static const char *
get_key(void)
{
    static char *name = NULL;
    if (!name) {
        static char hname[] = &quot;foobar&quot;;
        name = &amp;hname[0];
    }
    return name;
}

int
main()
{
    unordered_map&lt;const char *, int&gt; map{{&quot;foobar&quot;,3}};

    fprintf(stderr, &quot;same? %d\n&quot;, string(&quot;foobar&quot;) == get_key());

    fprintf(stderr, &quot;val = %d\n&quot;, map[&quot;foobar&quot;]);
    fprintf(stderr, &quot;val = %d\n&quot;, map[get_key()]);

    const char * name = get_key();
    fprintf(stderr, &quot;val = %d\n&quot;, map[name]);

    return 0;
}

// Ausgabe:
// same? 1
// val = 3
// val = 0
// val = 0
</code></pre>
<p>Die Ausgabe findet ihr am Ende der Auflistung. <strong>val</strong> sollte meiner Ansicht nach immer &quot; = 3&quot; in der Ausgabe haben. Aber wenn ich statt einer Zeichenkonstanten wie im ersten Fall einen Zeiger der auf ein statisches Array (siehe Funktion get_key) zeigt habe, dann findet op[] den Schlüssel nicht. Ändere ich get_key wie folgt ab, dann klappt alles:</p>
<pre><code class="language-cpp">static const char *
get_key(void)
{
    static char *name = &quot;foobar&quot;;
    return name;
}
</code></pre>
<p>Ich kompiliere den Code wie folgt:<br />
<strong>g++ -std=c++0x -o foo <a href="http://foo.cc" rel="nofollow">foo.cc</a></strong></p>
<p>Aber auch mit Änderungen, wie z.B. -O0 ändert sich an dem Verhalten nichts. Ich habe übrigens C++11 verwendet damit der Beispiel-Code kürzer wird (abgesehen von der unordered_map). Das Original initialisiert die map &quot;klassisch&quot;.</p>
<p>Die GCC Version ist g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3</p>
<p>Habt ihr eine Idee woran das liegen kann?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/309326/seltsames-verhalten-bei-benutzung-von-op-mit-const-char-als-schlüssel</link><generator>RSS for Node</generator><lastBuildDate>Wed, 05 Aug 2026 06:36:56 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/309326.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 19 Oct 2012 18:47:11 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Seltsames Verhalten bei Benutzung von op[] mit const char * als Schlüssel on Fri, 19 Oct 2012 18:47:11 GMT]]></title><description><![CDATA[<p>Hi,</p>
<p>ich habe mein Problem auf folgendes Stück Code reduzieren können:</p>
<pre><code class="language-cpp">#include &lt;unordered_map&gt;
#include &lt;string&gt;
#include &lt;cstdio&gt;

using namespace std;

static const char *
get_key(void)
{
    static char *name = NULL;
    if (!name) {
        static char hname[] = &quot;foobar&quot;;
        name = &amp;hname[0];
    }
    return name;
}

int
main()
{
    unordered_map&lt;const char *, int&gt; map{{&quot;foobar&quot;,3}};

    fprintf(stderr, &quot;same? %d\n&quot;, string(&quot;foobar&quot;) == get_key());

    fprintf(stderr, &quot;val = %d\n&quot;, map[&quot;foobar&quot;]);
    fprintf(stderr, &quot;val = %d\n&quot;, map[get_key()]);

    const char * name = get_key();
    fprintf(stderr, &quot;val = %d\n&quot;, map[name]);

    return 0;
}

// Ausgabe:
// same? 1
// val = 3
// val = 0
// val = 0
</code></pre>
<p>Die Ausgabe findet ihr am Ende der Auflistung. <strong>val</strong> sollte meiner Ansicht nach immer &quot; = 3&quot; in der Ausgabe haben. Aber wenn ich statt einer Zeichenkonstanten wie im ersten Fall einen Zeiger der auf ein statisches Array (siehe Funktion get_key) zeigt habe, dann findet op[] den Schlüssel nicht. Ändere ich get_key wie folgt ab, dann klappt alles:</p>
<pre><code class="language-cpp">static const char *
get_key(void)
{
    static char *name = &quot;foobar&quot;;
    return name;
}
</code></pre>
<p>Ich kompiliere den Code wie folgt:<br />
<strong>g++ -std=c++0x -o foo <a href="http://foo.cc" rel="nofollow">foo.cc</a></strong></p>
<p>Aber auch mit Änderungen, wie z.B. -O0 ändert sich an dem Verhalten nichts. Ich habe übrigens C++11 verwendet damit der Beispiel-Code kürzer wird (abgesehen von der unordered_map). Das Original initialisiert die map &quot;klassisch&quot;.</p>
<p>Die GCC Version ist g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3</p>
<p>Habt ihr eine Idee woran das liegen kann?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2262155</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2262155</guid><dc:creator><![CDATA[Toni R.]]></dc:creator><pubDate>Fri, 19 Oct 2012 18:47:11 GMT</pubDate></item><item><title><![CDATA[Reply to Seltsames Verhalten bei Benutzung von op[] mit const char * als Schlüssel on Fri, 19 Oct 2012 18:54:37 GMT]]></title><description><![CDATA[<p>Toni R. schrieb:</p>
<blockquote>
<p>Habt ihr eine Idee woran das liegen kann?</p>
</blockquote>
<p>Eh.. ja. Du solltest der map vielleicht mitteilen, dass sie da strings vergleichen soll, und nicht nur Pointer. Eigentlich offensichtlich, du schlägst dir bestimmt gerade vor die Stirn. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f921.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--clown_face"
      title=":clown:"
      alt="🤡"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2262156</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2262156</guid><dc:creator><![CDATA[cooky451]]></dc:creator><pubDate>Fri, 19 Oct 2012 18:54:37 GMT</pubDate></item><item><title><![CDATA[Reply to Seltsames Verhalten bei Benutzung von op[] mit const char * als Schlüssel on Fri, 19 Oct 2012 18:58:53 GMT]]></title><description><![CDATA[<p>Das Problem lässt sich sogar noch weiter reduzieren, nämlich darauf:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;cstring&gt;
using namespace std;

int main()
{
  const char foo[] = &quot;foobar&quot;;
  const char bar[] = &quot;foobar&quot;;

  cout &lt;&lt; (foo == bar) &lt;&lt; endl;  // -&gt; keine Gleichheit
  cout &lt;&lt; (strcmp(foo, bar) == 0) &lt;&lt; endl;  // Gleichheit
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2262159</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2262159</guid><dc:creator><![CDATA[Michael E.]]></dc:creator><pubDate>Fri, 19 Oct 2012 18:58:53 GMT</pubDate></item><item><title><![CDATA[Reply to Seltsames Verhalten bei Benutzung von op[] mit const char * als Schlüssel on Fri, 19 Oct 2012 19:01:17 GMT]]></title><description><![CDATA[<p>cooky451 schrieb:</p>
<blockquote>
<p>Toni R. schrieb:</p>
<blockquote>
<p>Habt ihr eine Idee woran das liegen kann?</p>
</blockquote>
<p>Eh.. ja. Du solltest der map vielleicht mitteilen, dass sie da strings vergleichen soll, und nicht nur Pointer. Eigentlich offensichtlich, du schlägst dir bestimmt gerade vor die Stirn. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f921.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--clown_face"
      title=":clown:"
      alt="🤡"
    /></p>
</blockquote>
<p>Oh Mann..., eigentlich klar. Kommt vermutlich davon, dass ich gerade in Python ein C++ Code-Generator schreibe. Da ist mein Gehirn wohl gerade im falschen Modus <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="😉"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2262160</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2262160</guid><dc:creator><![CDATA[Toni R.]]></dc:creator><pubDate>Fri, 19 Oct 2012 19:01:17 GMT</pubDate></item><item><title><![CDATA[Reply to Seltsames Verhalten bei Benutzung von op[] mit const char * als Schlüssel on Fri, 19 Oct 2012 19:07:45 GMT]]></title><description><![CDATA[<p>Toni R. schrieb:</p>
<blockquote>
<p>Oh Mann..., eigentlich klar. Kommt vermutlich davon, dass ich gerade in Python ein C++ Code-Generator schreibe. Da ist mein Gehirn wohl gerade im falschen Modus <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="😉"
    /></p>
</blockquote>
<p>Na ja nicht zwangsläufig, wenn du einfach std::string nehmen würdest..</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2262162</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2262162</guid><dc:creator><![CDATA[cooky451]]></dc:creator><pubDate>Fri, 19 Oct 2012 19:07:45 GMT</pubDate></item></channel></rss>