Jump to content
  • Advertisement

Archived

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

prgrmmr

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

This topic is 5770 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

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
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..

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
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
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!