• Advertisement

Archived

This topic is now archived and is closed to further replies.

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

This topic is 5499 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Yes, that is. You can use FLOAT instead of FLOAT, LONG instead of long, but: if you use ''bool'' the you use a logical value, when you use ''BOOL'' it is treated as a 32-bit integer. (INT) sorry if you has already known it, but I discovered it some days before, and found it interesting.

Share this post


Link to post
Share on other sites
Advertisement
BOOL is the same as bool if I do this:

typedef bool BOOL;

It really just depends on however it is defined, since BOOL is not standard C or C++

Share this post


Link to post
Share on other sites
i mean the general definitions, and C++. I know the typedef
but I believe that ( until know ) that they are the same.

so I thought there is already a typedef bool BOOL; in some header file, because there is for the other types. So if you write long, or LONG they''re the same.

and the only reason I posted it, because I may help other programmers, who doesn''t know this, I wonder how can the size a logical value be 4 bytes.

Share this post


Link to post
Share on other sites
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. If your compiler has optimizing on, this code would generate the same 8 byte structure:


struct A
{
bool var1;
int var2;
};

struct B
{
BOOL var1;
int var2;
}


The int takes four bytes, but since the bool''s 1 byte offset would make the int''s position not evently divisible by 4 in memory, the compiler instead adds a 3 byte padding in between. In some speed-sensitive cases there is therefore a logical use of BOOL, although it may seem unnecessary because the compiler can handle it.

I''d recommend though that you place your 1 byte bools last in your structures and group them together with 1 byte chars and the like. That way you''ll not add unnecessary padding space.

Share this post


Link to post
Share on other sites
quote:
Original post by Kippesoep
This is because there is no C datatype known as bool, that''s a C++ datatype.


Wasn''t bool added as a datatype recently in C? Anyways, at least there was no bool datatype in C when BOOL was typedefed.

Share this post


Link to post
Share on other sites
quote:
Original post by Brother Bob
Wasn't bool added as a datatype recently in C? Anyways, at least there was no bool datatype in C when BOOL was typedefed.



Yeah, in C99 the keyword is _Bool.

500 Error.

[edited by - microdot on January 31, 2003 11:02:34 AM]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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..

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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 )

Share this post


Link to post
Share on other sites
*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....

Share this post


Link to post
Share on other sites
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
};

Share this post


Link to post
Share on other sites
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;

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
I believe that usertype.dat is just a text file with a list of keywords on each line.

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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites

  • Advertisement