Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ 'true' and 'false' keywords suddenly not true or false in Visual C++ 6.0

Tags:

c++

visual-c++

My compiler (VC++ 6.0 sp6) has apparently gone insane. In certain pieces of code I'm seeing that 'bool mybool = true;' evalutes to and assigns false, and vice versa for true. Changing the true/false keywords to 1/0 makes it work fine. The same code compiles elsewhere fine without changing the true/false keywords.

What could possibly cause this? My first thought was RAM or disk corruption, but that all checked out fine. I'm not far from reformatting my drive and reinstalling everything, but I'm terrified I'd still see the same misbehavior.

Is it even technically possible for a macro or linked-in library somewhere to screw up the meaning of 'true' and 'false'?

UPDATE: Mystery solved. An environment variable flag on my machine was set to 'false' and the way this was interpolated by some preprocessor code redefined the keyword.

like image 427
kingkongrevenge Avatar asked Feb 04 '09 00:02

kingkongrevenge


5 Answers

A preprocessor macro could certainly do it, although that would be pretty surprising. One way to check if that is the case would be

#ifdef true
#  error "true is defined as a macro"
#endif
#ifdef false
#  error "false is defined as a macro"
#endif

Response to comments:

Find a non-header file where you see this behavior, preferably one with few #includes.

In the middle of the list of includes, put the #ifdef #error directives.

if the error trips you know it's in the first half of includes, if it doesn't it's in the second half. Split the half in half and repeat. When you narrow it down to one header, open that header. If that header includes any headers repeat the process for the list of headers it includes. Eventually you should be able to find the #defines . Tedious, I agree.

like image 81
Logan Capaldo Avatar answered Oct 31 '22 18:10

Logan Capaldo


Buffer overflows and writing into uninitialized memory can also account for such behavior. For example, if you have an array and bool allocated in adjacent memory locations and accidentally write beyond the bounds of the array.

like image 37
codelogic Avatar answered Oct 31 '22 19:10

codelogic


If you're seeing this problem when checking variable values in the debugger, and you're running a release build, it could just be an artifact of the debugging system. The easiest way to check this would be to add some code along the lines of:

if (mybool)
    printf("mybool is true\n");
else
    printf("mybool is false\n");

You'll be able to quickly identify whether the debugger is accurate or not.

like image 5
tsellon Avatar answered Oct 31 '22 18:10

tsellon


First, VC6 is ancient and buggy. There are cases where it just generates incorrect code, which could be the answer to your problem. Don't use VC6 if you have a choice.

Second, what you're seeing is most likely just the result of optimizations. Does your code behave correctly? If so, it's just that the debugger gets confused because the code that is executed is different (due to optimizations) than the source code.

like image 4
jalf Avatar answered Oct 31 '22 17:10

jalf


Most likely a header file is flipping the values via macro - although why I could not hazard a guess. Any chance that file is being compiled as C and others as C++ and there's some #ifdef/#define code somewhere that is attempting to 'fix' the true/false values, but got them wrong?

like image 3
Joe Avatar answered Oct 31 '22 17:10

Joe