Visual debbuger displays "true" as "0",

A very strange case when it just happened to me - when using the debugger, it shows me that the value of some logical variable is true , but when I print it (or do any other operation with it), it behaves like 0 (i.e. false).

What can I do to fix this error? I'm afraid this is an environmental mistake, so publishing the sample code will be pointless. (a hidden, annoying memory management error cannot be the cause, right?), and in this context I want to note that it will surprise me when I find that my environment is poorly configured (I have been working on this project for more than a year now).

So far, I:

  • It is checked twice that the compiler does not do any optimization for the code in my project properties.
  • Tried to re-open the visual studio and clean and rebuild the project
  • Search the Internet for solutions (not found). I am using visual studio 2010, and my programming language is C ++

Regarding code sharing requests:

Sorry, but I canโ€™t post the code (my bosses will not be happy to see it running on the Internet). If you can give some ideas about the possible causes of the problem, it will be great, and I will look for the keys in the code myself to check if these causes caused this problem. However, to be clear, here are a few code lines that fight me:

  bool dir = getNode(location)->getNext()->getDirection(); //dir is displayed as "true" in the debbuger int toPush = (dir == 1) ? 1 : 0; //"toPush" is displayed as "0" in the debbugger cout<<dir<<endl; //both output 0. cout<<(dir == true)<<endl; 

Following your request, I am enclosing a screen shot. Pay attention to the value of "dir", which is deactivated on the right side of the screen as "true", and the output of the program on the right, which ends with 0 (which corresponds to the command "cout <dir").

screenshot

+4
source share
1 answer

You should not use the == operator to check the bool value for truth. Any nonzero value is true. You have two lines of cout , and the last two lines in the console window say 240 and 0. I wrote this to demonstrate what I think is happening:

 #include <iostream> using namespace std; static bool getDirection() { union forceBoolValue { unsigned int iValue; bool bValue; }; forceBoolValue retValue; retValue.iValue = 0xFFFFFFFF; return retValue.bValue; } int _tmain(int argc, _TCHAR* argv[]) { bool dir = getDirection(); //dir is now 255*, which is non-zero and therefore "true" int toPush = (dir == 1) ? 1 : 0; //dir may be true but it is not one, so toPush is 0 int toPush2 = dir ? 1 : 0; //dir is true, so toPush2 is 1 cout << "Dir: " << dir << endl; cout << "toPush: " << toPush << endl; cout << "toPush2: " << toPush2 << endl; return 0; } 

A similar situation occurs with dir == true , where it is likely to test the unit value again. I have no idea why dir gets an unusual value in your code (240), but if you remove the comparisons and just check the value (like toPush2 above), it should fix the problem.

I know that you said that the toPush line toPush intended to demonstrate the problem, but are you doing any comparisons with any of your real codes? If so, delete them.

* dir may not be 255, depending on the size of the bool in your environment.

+3
source

All Articles