Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Why using BOOL over bool (or not)

Why using BOOL over bool (or not)

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuresperformancequestionlearningworkspace
9 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    EiSl
    wrote on last edited by
    #1

    Hello all, I was just wondering about the typical difference in usage between type BOOL and bool. Within the Windows environment, the BOOL is an unsigned integer. When using data-structures you find the bool used iso BOOL, which is put inside a kind of bit-array by the compiler. This is of course logical due memory usage, since the BOOL takes in Win32 4 bytes. Is there any reason to use the BOOL over the bool type definition? Thanks in advance, EiSl

    J C 2 Replies Last reply
    0
    • E EiSl

      Hello all, I was just wondering about the typical difference in usage between type BOOL and bool. Within the Windows environment, the BOOL is an unsigned integer. When using data-structures you find the bool used iso BOOL, which is put inside a kind of bit-array by the compiler. This is of course logical due memory usage, since the BOOL takes in Win32 4 bytes. Is there any reason to use the BOOL over the bool type definition? Thanks in advance, EiSl

      J Offline
      J Offline
      Joaquin M Lopez Munoz
      wrote on last edited by
      #2

      bool is a C++ keyword (which BOOL is not) and refers to a built-in C++ type (does not exist in C) for boolean values. In the particular MS implementation, bool is 1 byte wide, which could matter if memory efficiency is highly valued. Nevertheless, the main reason to prefer bool over BOOL is type efficiency: you cannot do any funky integer arithmetic with bools, like adding or multiplying them, which is good. To sum it up, I'd use bool if:

      1. I'm programming in C++ (does not work in C)
      2. I'm not dealing with a previous piece of code that uses BOOLs (like the API definitions in <windows.h>),

      and use BOOL in the remaining cases. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

      B 1 Reply Last reply
      0
      • E EiSl

        Hello all, I was just wondering about the typical difference in usage between type BOOL and bool. Within the Windows environment, the BOOL is an unsigned integer. When using data-structures you find the bool used iso BOOL, which is put inside a kind of bit-array by the compiler. This is of course logical due memory usage, since the BOOL takes in Win32 4 bytes. Is there any reason to use the BOOL over the bool type definition? Thanks in advance, EiSl

        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #3

        As has been said, BOOL exists for C API programming, where no bool exists. It is a BAD idea to use it if you can avoid it. To get rid of warnings when converting BOOL to bool do this: bool bResult = (::SomeDodgyAPIFunction()>0); Christian I have come to clean zee pooollll. - Michael Martin Dec 30, 2001

        Sonork ID 100.10002:MeanManOz

        I live in Bob's HungOut now

        1 Reply Last reply
        0
        • J Joaquin M Lopez Munoz

          bool is a C++ keyword (which BOOL is not) and refers to a built-in C++ type (does not exist in C) for boolean values. In the particular MS implementation, bool is 1 byte wide, which could matter if memory efficiency is highly valued. Nevertheless, the main reason to prefer bool over BOOL is type efficiency: you cannot do any funky integer arithmetic with bools, like adding or multiplying them, which is good. To sum it up, I'd use bool if:

          1. I'm programming in C++ (does not work in C)
          2. I'm not dealing with a previous piece of code that uses BOOLs (like the API definitions in <windows.h>),

          and use BOOL in the remaining cases. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

          B Offline
          B Offline
          Bernhard
          wrote on last edited by
          #4

          another question: why should i use if (pointer == NULL) and not if (pointer == 0) is there any difference between those two ? is this historically? does NULL has got any advantages? thanks in advance bernhard


          Sometimes I think the surest sign for intelligent life elsewhere in the universe is that none of them ever tried to contact us.

          J 1 Reply Last reply
          0
          • B Bernhard

            another question: why should i use if (pointer == NULL) and not if (pointer == 0) is there any difference between those two ? is this historically? does NULL has got any advantages? thanks in advance bernhard


            Sometimes I think the surest sign for intelligent life elsewhere in the universe is that none of them ever tried to contact us.

            J Offline
            J Offline
            Joaquin M Lopez Munoz
            wrote on last edited by
            #5

            Well, I'd tend to use the second form (if (pointer == 0)) because "0" is the standard name for the null pointer since C was invented. NULL is a macro of dubious reputation included in stddef.h and stdio.h, altough MS folks also provide it in windows.h for your convenience. Why do I prefer unadorned 0 instead of NULL? Most of the time it doesn't really matter which syntax you use, and NULL is admittedly a bit clearer, but the problem with this macro is that its definition can vary from compiler vendor to compiler vendor (some define it as a bare 0, others as ((void *)0)). Worse yet, as this macro tends to be defined independently by different library writters (as it is the case in windows.h) ocassionally you can end up with different NULL definitions across compilation units. In extreme pathological situations this can make a difference. Another, more practical, reason to prefer 0 over NULL is that you don't have to rely on any previously included header. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

            B T 2 Replies Last reply
            0
            • J Joaquin M Lopez Munoz

              Well, I'd tend to use the second form (if (pointer == 0)) because "0" is the standard name for the null pointer since C was invented. NULL is a macro of dubious reputation included in stddef.h and stdio.h, altough MS folks also provide it in windows.h for your convenience. Why do I prefer unadorned 0 instead of NULL? Most of the time it doesn't really matter which syntax you use, and NULL is admittedly a bit clearer, but the problem with this macro is that its definition can vary from compiler vendor to compiler vendor (some define it as a bare 0, others as ((void *)0)). Worse yet, as this macro tends to be defined independently by different library writters (as it is the case in windows.h) ocassionally you can end up with different NULL definitions across compilation units. In extreme pathological situations this can make a difference. Another, more practical, reason to prefer 0 over NULL is that you don't have to rely on any previously included header. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

              B Offline
              B Offline
              Bernhard
              wrote on last edited by
              #6

              thank you for your quick answer.. the only reason why i'm asking this is that most of the programmers with c - background i know use the NULL for pointers.. i just use 0 and wanted to ask, before i make a real flaw all over my code (i am a little bit a pointer-o-holic) thanks bernhard


              Sometimes I think the surest sign for intelligent life elsewhere in the universe is that none of them ever tried to contact us.

              1 Reply Last reply
              0
              • J Joaquin M Lopez Munoz

                Well, I'd tend to use the second form (if (pointer == 0)) because "0" is the standard name for the null pointer since C was invented. NULL is a macro of dubious reputation included in stddef.h and stdio.h, altough MS folks also provide it in windows.h for your convenience. Why do I prefer unadorned 0 instead of NULL? Most of the time it doesn't really matter which syntax you use, and NULL is admittedly a bit clearer, but the problem with this macro is that its definition can vary from compiler vendor to compiler vendor (some define it as a bare 0, others as ((void *)0)). Worse yet, as this macro tends to be defined independently by different library writters (as it is the case in windows.h) ocassionally you can end up with different NULL definitions across compilation units. In extreme pathological situations this can make a difference. Another, more practical, reason to prefer 0 over NULL is that you don't have to rely on any previously included header. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

                T Offline
                T Offline
                Tim Smith
                wrote on last edited by
                #7

                Using NULL or 0 is basically a style issue of religious proportions. After reading your message, I did a little research on NULL and 0. Here are some interesting facts. 1. NULL is usually defined as 0 or ((void *) 0). The later being an valid alternative to help prevent people from doing stupid stuff like assigning NULL to a non-pointer variable. This is in fact not at all portable. 2. When NULL and 0 are used in the context of pointers, they don't actually mean that the pointer is zero. The C and C++ standards state that the actual value stored in a pointer to represent NULL or 0 is machine depended and need not be zero. It is the compiler's job to make sure that comparisons against NULL or 0 perform the proper tests. Tim Smith Descartes Systems Sciences, Inc.

                J 1 Reply Last reply
                0
                • T Tim Smith

                  Using NULL or 0 is basically a style issue of religious proportions. After reading your message, I did a little research on NULL and 0. Here are some interesting facts. 1. NULL is usually defined as 0 or ((void *) 0). The later being an valid alternative to help prevent people from doing stupid stuff like assigning NULL to a non-pointer variable. This is in fact not at all portable. 2. When NULL and 0 are used in the context of pointers, they don't actually mean that the pointer is zero. The C and C++ standards state that the actual value stored in a pointer to represent NULL or 0 is machine depended and need not be zero. It is the compiler's job to make sure that comparisons against NULL or 0 perform the proper tests. Tim Smith Descartes Systems Sciences, Inc.

                  J Offline
                  J Offline
                  Joaquin M Lopez Munoz
                  wrote on last edited by
                  #8

                  I agree with you in that this is a very tiny issue that receives a disproportionate amount of interest. With respect to your fact #1, there's an interesting associated problem: the following line of code

                  char *p=(void*)0;

                  is legal C, but illegal in C++, as this latter language forbids implicit casting from void* to any other pointer type, due to type safety reasons. So even this seemingly innocent definition for NULL has unexpected side effects. This surprising fact and a lot more info about the null pointer you can find it on Stroustrup's The Design and Evolution of C++, §11.2.3. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

                  T 1 Reply Last reply
                  0
                  • J Joaquin M Lopez Munoz

                    I agree with you in that this is a very tiny issue that receives a disproportionate amount of interest. With respect to your fact #1, there's an interesting associated problem: the following line of code

                    char *p=(void*)0;

                    is legal C, but illegal in C++, as this latter language forbids implicit casting from void* to any other pointer type, due to type safety reasons. So even this seemingly innocent definition for NULL has unexpected side effects. This surprising fact and a lot more info about the null pointer you can find it on Stroustrup's The Design and Evolution of C++, §11.2.3. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

                    T Offline
                    T Offline
                    Tim Smith
                    wrote on last edited by
                    #9

                    Ah, good point that I forgot about. I found this in WINDEF.H.

                    #ifndef NULL
                    #ifdef __cplusplus
                    #define NULL 0
                    #else
                    #define NULL ((void *)0)
                    #endif
                    #endif

                    I need to check that reference out. Thanks. Tim Smith Descartes Systems Sciences, Inc.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups