Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: bool versus BOOL in C++

Author: Omid David Tabibi

Date: 06:15:31 09/26/03

Go up one level in this thread


On September 26, 2003 at 07:59:42, Bo Persson wrote:

>On September 26, 2003 at 06:50:59, Gerd Isenberg wrote:
>
>>Hi All,
>>
>>in C++ we have the boolean type "bool" with the value range true/false.
>>I'm not sure about ANSI-C.
>
>There is a _Bool type in C99. If you include bool.h you get the usual typedefs
>etc for bool, true, and false.
>
>>
>>Due to some C-related "portability" problems and possible performance drawbacks
>>due sizeof(bool) == 1 implies partial register handling or zero extending to
>>native word lenght, you find in most MS-sources an "own" boolean type BOOL:
>>
>>typedef int BOOL; // e.g. WINDEF.H
>
>The reason for this is not performance, but that Windows is older than standard
>C and C++. There was no bool type back then.

But isn't a word sized variable preferable to a 1 byte variable for performance?

The reason why int doesn't have a specific size is that on each system int was
meant to be the fastest type of variable, word sized. That's why using int is
better than char even for small numbers (except when having large arrays where
int would waste too much cache). For the same reason I think typedef int BOOL
would be better than bool when having only speed in mind.

P.S. It seems that on 64 bit systems sizeof(int) will still be 4, not word
sized. Wouldn't an 8 byte int be faster than a 4 byte one on a 64 bit system?!


>
>You don't really have to extend the value to test for zero/non-zero. If you have
>several bools in a struct, the smaller size could be good too.
>
>>
>>This own BOOL type is of course not "typesafe" as bool, as you may assign any
>>"int" expressions to it. With BOOL one should interprete zero as FALSE and any
>>other value as TRUE. Due to this ambiguity, comparing BOOL-expressions with TRUE
>>may be erroneous, so better compare with != FALSE.
>
>There are also potential overload problems.
>
>>
>>See also this bugreport related to this issue:
>>
>>http://www.codeproject.com/buglist/virtualboolbug.asp
>>
>>Actually i have some "disputes" with colleagues obout it.
>>I found bool better for didactical reasons,
>>but stay with BOOL for pragmatical reasons.
>>
>>A few questions:
>>
>>Is sizeof(bool) == 1 per definition,
>>or is it compiler implementation depending?
>
>That is up to the compiler.
>
>
>>
>>Is there any conditional preprocessor directive to ask whether a user defined
>>type is already typedefined, similar to #ifdef?
>
>No.
>
>>
>>What is your opinion / experience with bool versus BOOL?
>
>I use bool as much as possible. BOOL is just for interfacing with Windows.
>
>
>>
>>Thanks in advance for your suggestions,
>>Gerd
>
>
>Bo Persson



This page took 0.01 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.