Did You Know? that BOOL is not the same as bool?

Started by
18 comments, last by prgrmmr 21 years, 2 months ago
quote:Original post by Metal Typhoon
wow i didnt know ! i'll use bool now :D


Be careful. Remember that a BOOL is a totally different type than bool. Switching all your BOOLs to bool will cause bad results with certain aspects of Win32. For instance, using bool as a return type for a DlgProc function (instead of BOOL) will cause very bad results with the dialog box.

Except for places where Win32/Microsoft requires BOOL, I only use bool. It is standard C++ and is colored a pretty blue in VC++ .

-Mike

[edit: typos]

[edited by - doctorsixstring on January 31, 2003 1:13:01 PM]
Advertisement
Yes.

BTW, you can make your own keywords pretty colors. Just put them in the "usertype.dat" file (you may have to make it yourself). You can even make them a different color than normal keywords (user keywords are a different section in options), but I don''t suggest it--eventually, it''ll look like someone puked in your IDE.

Think Liberally..
quote:Original post by Unwise owl
The reason there is a BOOL is because like someone said: bool does not exist in C so Microsoft needed a own type. However, as bool is only 1 byte long (in comparision to the 4 byte BOOL), it can cause misalignment among structure members.

I don't know about the #defined (and clearly implementation specific) BOOL, but there is in general no such exact guarantee about the size of a bool. The C++ standard guarantees that 1<=sizeof(bool)<=sizeof(long), but that's all - it could be 8 bits, it could be 32 bits (which, for speed reasons, might make sense), it could be larger still (keep in mind that we're not guaranteed that a char is 8 bits ...).

[edited by - Miserable on January 31, 2003 1:25:55 PM]
so what''s the better?
using BOOL( 4 bytes ) in a quadtree alogrithm or bool( 1 byte )
what''s faster.

i''d use it in a subdivision matrix( is that quadtree requies subdiv )
*LOL* ive never even heard of "BOOL". but then again i avoid the Win32 API at all cost in order to make portable code.

I really wish there was a Win32 forum, because there are alot of Win32/MFC api questions in the General programming forum....
I''d probably go with the bool, but I''d place it so it wouldn''t cause misalignment in the structs:

struct A{    bool boolean; //1 byte    char padding[3]; //3 bytes    int integer; //1 + 3 = 4 bytes offset = no misalignment}; 


As in the example shown above, you can manually add padding bytes to your struct to achieve maxiumum speed on the integer member. This is what the compiler does for you if you have optimization turned on. If you add your padding bytes yourself however, you get more "control" over your structure.

Still the best solution for adding bools is this one, where all members are sorted by size. Smaller members can not easily be misaligned. 1 byte members like the bool cannot be disaligned at all; they are put at the back.

struct B{    //4 byte vars here:    int integer1;    int integer2;    char *pointer1; //pointers are 4 bytes in Win32    char *pointer2;    //2 byte vars here:    short small1;    short array[10];    //1 bytes vars here:    bool boolean;    char string[100]; //none of these chars can be misaligned}; 

quote:Original post by Stoffel
BTW, you can make your own keywords pretty colors. Just put them in the "usertype.dat" file (you may have to make it yourself). You can even make them a different color than normal keywords (user keywords are a different section in options), but I don''t suggest it--eventually, it''ll look like someone puked in your IDE.
Hey, that''s a interesting? How do you do that? What do I have to put in usertype.dat? I am using VC.NET here.

return 0;
quote: What do I have to put in usertype.dat? I am using VC.NET here.


That sounds like a question that can be answered by looking at the documentation... which we all have access to.
I believe that usertype.dat is just a text file with a list of keywords on each line.

usertype.dat
------------
keyword1
keyword2
keyword3

The main problem with interchanging bool and BOOL how things are evaulated. With BOOL any value that''s non zero is TRUE. This means that 65534 would be a valid TRUE value. This is not the case with bool which explictly states that 0 is false and 1 is true.

The easiest way to avoid problems when mixing these two data types is to never do something like "if( Value == TRUE )" or "if( Value == true)". Instead do "if( Value != FALSE )" or "if( Value != false)". Because FALSE and false are gaurenteed to be defined as 0 this becomes a valid check for either BOOL and bool. This is typically good programming practice anyway.

This topic is closed to new replies.

Advertisement