I have a variable which I set early in the program:
double foo = 0.0 ;
The program continues running and later, without ever having touched foo
, it becomes some garbage value (say 6.953355807230268e-310
).
To track this down I have printed the address of foo
, say 0x7fffffffcfa0
, and then printed the value at that address with gdb
at various points until it has changed:
p *(double *) 0x7fffffffcfa0
Digging into each of the function calls, it seems to happen at the following line:
++bar[name];
Here bar
is a mutable std::map<std::string, unsigned int>
member variable of some class, and name
is a string.
In gdb
if I call p bar
it returns = std::map with 0 elements
.
Hence, calling bar[name]
inserts an element into the map.
If I call p bar[name]
in gdb
it gives 0 (expected) and the value of foo
is unchanged.
If I then call p ++bar[name]
, the value of foo
gets changed.
If I step into the line ++bar[name]
, I can track the change of foo
down to /usr/include/c++/11/bits/stl_map.h:497
which reads:
iterator __i = lower_bound(__k);
If I try to step into this function, it changes the value of foo
without any steps between.
At this point I am stumped as to what is going on and, unfortunately, I don’t know how to reproduce the problem with a simpler program. Any help is appreciated to try to diagnose and/or treat this issue. In case it is relevant, I’m using g++ 11.4.0
to compile.