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.
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.
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.
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.
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.
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?
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