I'm on a big C++/stl mystery with Visual Studio 2010 :
I push back an object to a list, and when I access to it, I get partially un-initialized data. Here is a very minimalist sample of code that produce this behavior :
#include <map>
#include <list>
#include <iostream>
using namespace std ;
struct Foo
{
Foo ( int x , int y ) ;
friend ostream & operator<< ( ostream & out , const Foo & foo ) ;
int x,y ;
};
Foo::Foo ( int x , int y ) : x(x),y(y)
{
cout << "construct " << *this << endl ;
}
ostream & operator<< ( ostream & out , const Foo & foo )
{
out << "Foo( " << foo.x << "," << foo.y << " )[" << &foo << "]";
return out ;
}
int main ()
{
list<Foo> foolist ;
for ( int x = 7 ; x < 12 ; x++ )
foolist.push_back( Foo( x,x*x )) ;
for ( list<Foo>::iterator i = foolist.begin() ; i != foolist.end() ; ++i )
cout << *i << endl ;
}
And here is the output :
construct Foo( 7,49 )[003FFE94]
construct Foo( 8,64 )[003FFE94]
construct Foo( 9,81 )[003FFE94]
construct Foo( 10,100 )[003FFE94]
construct Foo( 11,121 )[003FFE94]
Foo( 7,-33686019 )[00224D98]
Foo( 8,-33686019 )[00224DE0]
Foo( 9,-33686019 )[00225048]
Foo( 10,-33686019 )[00225090]
Foo( 11,-33686019 )[002250D8]
I think that 003FFE94 is the address of a temporary object which is copied at its final location within the list. But the copy seems to be partial : only the first member x is copied.
For information, the same code works well when compiled with gcc :
construct Foo( 7,49 )[0x28ff20]
construct Foo( 8,64 )[0x28ff20]
construct Foo( 9,81 )[0x28ff20]
construct Foo( 10,100 )[0x28ff20]
construct Foo( 11,121 )[0x28ff20]
Foo( 7,49 )[0x976f40]
Foo( 8,64 )[0x976f58]
Foo( 9,81 )[0x976f70]
Foo( 10,100 )[0x976f88]
Foo( 11,121 )[0x976fa0]
So, if someone understands what Visual is doing with my list, and what I should do to make it work... Thanks in advance !
Hadrien