views:

96

answers:

3

I'm debugging part of a large project in Visual Studio 2005, and stepping through the code line by line.

int speed = this->values.speed;
int ref = this->values.ref_speed;

After stepping past the first line, values.speed has a value of 61, but for some reason, speed is getting assigned the value 58. After the second line, values.ref_speed has a value of 58, but ref gets assigned the value 30.

When paused, you can see that the original values are in fact 61 and 58 respectively, but the values getting stored are different.

What is causing this behaviour?

A: 

Are you doing this in debug or release mode? If you're doing it in release mode, or with optimization on, the compiler may have reordered things for greater performance, and that can lead to odd-looking results. This doesn't mean that anything is wrong in the actual execution, but it can be confusing to step through.

David Thornley
This happens in Debug mode.
Andrei Krotkov
+1  A: 

Crazy. Any chance you have a local variable of the same type as this->values whose name is also values (which would explain why you are referencing a member via this->)?

sean e
No local variables. I used this to clarify that it's not a local variable in a very long show() routine.
Andrei Krotkov
+6  A: 

This could happen if the definition of the values structure got changed in a header file and not all the object files got recompiled. Then the "map" of the structure your code in this file is using might not match the rest of the code's. That could explain why one of the variables appears to have the other's value.

Or the Visual Studio .pdb file didn't get updated for some reason, and Visual Studio is looking in the old place for the variable.

David
I would look into making sure your debugging info is synced properly with your project. I've had issues before where values weren't lining up due to older versions of debugging info being loaded.
Ron Warholic
Or if the structure was defined as two different things in two different places. I have no idea how it managed to compile, but the structure was defined twice in two places - with the orders of the variables swapped. Thanks!
Andrei Krotkov