I have a vector of pairs as a field inside an object. Said object has a method where I need to access values in the pairs in the vector. I am using an iterator to point to the place in the vector I wish to access. Here are snippets of code that contain the vector:
In header file:
vector<pair<double, double> > points;
vector<pair<double, double> >::iterator headingTo;
In constructor:
points.push_back(make_pair(1700.00, 3300.00));//Plus 20 or so other values
headingTo = points.begin();
In method:
double x = headingTo->first - positionX;
double y = headingTo->second - positionY;
However when I run this code y is not being created. Its not shown in Visual Studio at all when I use a break point to see the variables. However if I swap the lines around, y is accessible and x is not. Any ideas?
Edit: I've found the following works:
double headingToX = headingTo->first;
headingToX -= positionX;
double headingToY = headingTo->second;
headingToY-= positionY;
Check that you're not debugging an optimized build of the program - in that case the compiler is free to elide variables if it can determine that it has no need for them to produce the required outputs of the program.
However, even if you're using a non-optimized build, thie behavior can occur if you really don't use the variable at all. In this bug report on the Express version of VC++ 2010, you'll see the following comment made by a Microsoft representative (emphasis added):
Is this issue only occuring on variable that you have not used in any way other than assigning a value? The presence of the variable in the .pdb via inspection with a hex-editor, or seeing a corresponding "mov" instruction in the dissassembly does not guarantee that the compiler did not do some level of optimzation that prevents the debugger from inspecting the variable (the compiler always does small optimizations even in a debug build). The debugger can only guarantee access to a variable will be provided by the compiler if the variable is used in the application for another purpose other than being assigned a value.
If you have a repro where the variable is being used (other than being assigned a value) and you cannot inspect it in the debugger please let us know. Otherwise, this is an artifact of compiler optimizations.
It's unclear from the code that you posted whether or not you later use the value of y.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With