Here is the code excerpt.
std::map<double, double> temp;
temp[0] = .1;
cout << temp[1] << endl;
// result varies based on compiler
I'm compiling using the GCC version 4.4.1 and I get a value of 0 from temp[1]
, as I expect. My co-worker is compiling on GCC version 4.5.1. In debug mode (with the -g
flag), he gets 1000
. When compiling release mode (-O2
flag), he gets 0
.
My thought is that this is the type of issue that typically arises with uninitialized variables, except that maps are supposed to call the default constructor on their elements, based on this question and several others like it.
Moreover, The C++ Standard Library by Josuttis, states that
If you use a key as the index, for which no element yet exists, a new element gets inserted into the map automatically. The value of the new element is initialized by the default constructor of its type.
Why are elements in the map not being initialized in GCC 4.5.1 in debug mode? Am I not understanding what others have said about this behavior correctly? Is the default construction of new elements something that is not necessarily part of the standard? Or could this be an actual bug in the compiler?