Jump to content

  • Log In with Google      Sign In   
  • Create Account

Booleans show up with numeric values in the VS debugger

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 GuyWithBeard   Members   -  Reputation: 764


Posted 20 October 2012 - 07:40 AM


While debugging my engine I found that some boolean values in the Visual Studio 2012 debugger have a numeric value when they are true.

In my watch window I have an array of game object transforms, which looks like this:

+[0]{updated=true (200) position={x=1.25704611e-034 y=-431602080. z=-431602080. } orientation=...}
+[1]{updated=false position={x=-0.000000000 y=-0.000000000 z=-0.000000000 } orientation={w=0.000000000 ...} ...}
+[2]{updated=true (205) position={x=-431602080. y=-431602080. z=0.000000000 } orientation={w=...} ...}
+[3]{updated=false position={x=-0.000000000 y=0.000000000 z=-1.30761719 } orientation={w=0.000000000 ...} ...}
+[4]{updated=true (205) position={x=0.000000000 y=4.344e-044#DEN z=-431602080. } orientation=...}
+[5]{updated=false position={x=-1.30761719 y=0.000000000 z=0.000000000 } orientation={w=0.000000000 ...} ...}
+[6]{updated=true (31) position={x=-431602080. y=0.000000000 z=1.55368341e-027 } orientation=...}
+[7]{updated=false position={x=0.000000000 y=0.000000000 z=0.000000000 } orientation={w=-429916192. ...} ...}
+[8]{updated=false position={x=2.02258775e-027 y=-107372552. z=0.000000000 } orientation={w=15.3018999 ...} ...}
+[9]{updated=false position={x=0.000000000 y=-429916192. z=0.000000000 } orientation={w=1.23695956e-036 ...} ...}

I am confused by the numbers that appear after each "updated" variable that is true. Why are they not 1? The true/false value is correct, but I was under the impression that boolean values always have the value 1 or 0.

Have I misunderstood something?


#2 rip-off   Moderators   -  Reputation: 7622


Posted 20 October 2012 - 09:00 AM

In C++, bool is an integral type where sizeof(bool) is at least sizeof(char) - which is 1 byte of CHAR_BIT bits (i.e. at least 8 bits). Thus a C++ bool can actually contain values other than 0 or 1. Mostly this isn't a problem, as 0 is false and non-zero is true.

However, one problem comes in explicit comparisons:
if(updated == true) // might behave different from if(updated)
   // ...

Or even:
bool a = /* ... */, b = /* ... */;
if(a == b) {
   // ...

In Visual Studio, you'll can get warnings like the following from time to time:

warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning)

Now, the really interesting question is why your updated values are not "true" or "false". Without seeing the code, it is hard to tell. One thing is you might be doing something wrong where your memory is not being initialised or is being corrupted - I don't know if you are expecting position and orientation components like "-429916192.".

#3 Bacterius   Crossbones+   -  Reputation: 7969


Posted 20 October 2012 - 09:01 AM

Booleans are generally encoded as bytes (or some other word size) rather than bits when memory is not a constraint, because addressing individual bits in software is quite slow. So a boolean variable can actually potentially take values beyond 0 and 1. By convention, false is defined as zero, while anything else is defined as true.

Though this generally shouldn't happen - one thing that springs to mind is using bitwise operators instead of logical operators in your boolean comparisons, which is still logically correct in general but will produce results beyond 0 and 1. Another possibility is your stack/heap is getting trashed and your booleans are being overwritten with garbage, though if your game is working properly this is probably not the case.

Edited by Bacterius, 20 October 2012 - 09:04 AM.

The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.


- Pessimal Algorithms and Simplexity Analysis

#4 GuyWithBeard   Members   -  Reputation: 764


Posted 20 October 2012 - 10:24 AM

Thanks guys. As rip-off pointed out, there was other data not making any sense too. Turned out there was actually nothing wrong with my code, but I made a mistake when getting the data into the watch window. The transform was actually stored in a member with one uint32 and the transform, and I was accessing it as just a transform.

I did learn a bit more about bools from your posts though :)

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.