PDA

View Full Version : Interesting: bool != BOOL



lightatdawn
01-08-2002, 12:20 AM
Upon compiling some new code i discovered a disturbing error that troubled me for a bit.

error C2664: 'ResolveCollision' : cannot convert parameter 3 from 'int *' to 'bool *'

The thing is... parameter 3 is declared specifically as type "BOOL Expire". The function definition asks for "bool * Expire". So i pass "&Expire"... Not a problem right? Wrong. After perusing through the help files at first i find nothing:

Of course BOOL is a Win32 type but... Isn't a boolean a pretty universal concept? I mean come on...

Info on Data Types says simply:
BOOL A Boolean value.

Not very useful. Then i find this under "bool"

In Visual C++4.2, the Standard C++ header files contained a typedef that equated bool with int. In Visual C++ 5.0 and later, bool is implemented as a built-in type with a size of 1 byte. That means that for Visual C++ 4.2, a call of sizeof(bool) yields 4, while in Visual C++ 5.0 and later, the same call yields 1. This can cause memory corruption problems if you have defined structure members of type bool in Visual C++ 4.2 and are mixing object files (OBJ) and/or DLLs built with the 4.2 and 5.0 or later compilers.

So whats the reasoning behind this?

oskilian
01-08-2002, 12:29 AM
well actually, a BOOL (even if bigger) is faster than a bool, that's because a BOOL is 32 bits long and a bool is 8 bits long. Windows is 32 bit, and all of the mov's (assembly) are faster when using 32 bit registers, maybe you're wasting 24 bits (actually it's 31 bits that you're wasting), but it's faster!

Oskilian

adrianxw
01-08-2002, 02:44 AM
I remember looking ages for a completely baffling fault in one of my dialogs. It all seemed to work fine, but the edit boxes behaved very strangely, and had odd colours. One day I noticed that bool != BOOL AND that I had the dialog function declared the other type. Changed that and all problems dissappeared.

Unregistered
01-08-2002, 12:25 PM
BOOL is defined as an int, and TRUE and FALSE are defined as integer constants.

bool is a keyword and a primitive type added in C++, and true and false are the keywords for the values it may take on.

Granted, there SHOULD be an automatic BOOL to bool conversion (int to bool, in reality).

lightatdawn
01-08-2002, 02:35 PM
We have a very unique profession. Where else does one have to solve not only the man-made hurdles but also do combat against his own tools? :)

It all makes sense when you stop to think about it but as "BOOL" and "bool" behave identically in all other respects one would assume that they could be passed in the same manner.

>>Granted, there SHOULD be an automatic BOOL to bool conversion (int to bool, in reality).

Perhaps i should write a solution to that as i (for some odd reason) seem to use both in my code. But more reasonably i should get into some better habits and pick one.

no-one
01-09-2002, 11:09 AM
lightatdawn,

aim for obscurity and incompatibility,

define your own boolean as a float(maybe a custom sturct, hehe) or something and only use it!