tags:

views:

252

answers:

1

I simpled the example to help with reability. Error follows the code:

    #include <map>
    #include<iostream>
    #include<string>

    using namespace std;



    class A
    {

    public:
        map<pair<string, string>, string* > m;
        string str;

        A():str("sdfsd")
        {
         m[make_pair("aa","bb")]=&str;

        } 
        void foo()
        {
         map<pair<string, string>, string*>::iterator iter =m.begin();
         cout<<"In A::foo:\n";
         for(; iter!=m.end(); iter++)
         {
          pair<string, string> keys=iter->first;
          cout<<"map and test = "<<str<<" and "<<*iter->second<<endl;
         }
        } 
    };

    int main()
    {
    A a;
    a.foo();
    }

In core file:

 #0  0x0066f6c7 in std::string::_Rep::_M_grab () from
 /usr/lib/libstdc++.so.5
 #1  0x0066f80c in std::basic_string,
 std::allocator >::basic_string
 () from /usr/lib/libstdc++.so.5
 #2  0x087e1e69 in pair (this=0xbfff7070,
 _ctor_arg=@0x9c087d0) at (pair keys=iter->first;)
+1  A: 

Is it a compiler problem? It looks like some weird compiler optimisation, especially when aliasing iter->first makes the difference.

Just posted onto online compiler at http://codepad.org/ZhYvtj9v and it gave the correct result, without segfault.

Denis C