CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Jun 2002
    Location
    Germany
    Posts
    1,557

    The plain C equivalent of bool

    Gurus,

    As seen by my previous post, I am interfacing C to C++. In this endeavor a question arose as to the C equivalent of bool.

    How can one define bool to be used in structures which are shared between C++ and C files? How can one guarantee that the structures have the same size in the two languages?

    Maybe there is no safe solution.

    Thanks.

    Sincerely, Chris.



    Code:
    // In some header
    typedef int INT32;
    
    #if !defined(__cplusplus)
      typedef enum { false = 0, true  = 1 } bool;
    #endif
    
    // Is this structure guaranteed to have the same
    // size in C++ as well as C?
    typedef struct
    {
      INT32 n;
      bool  b;
    }
    t_data;
    You're gonna go blind staring into that box all day.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: The plain C equivalent of bool

    Originally posted by dude_1967
    Gurus,

    As seen by my previous post, I am interfacing C to C++. In this endeavor a question arose as to the C equivalent of bool.

    How can one define bool to be used in structures which are shared between C++ and C files?
    You don't. You use the same types shared by both languages. Since there is no "bool" type in C, you can't use it in your structure. When you write a structures or types to be used by both C and C++, you have to use the lowest common denominator, and that's 'C'. A C++ bool can be anything from an 8-bit char, to a 64 bit int. The size of bool is implementation defined (see ANSI/ISO C++ spec, section 5.3.3.1).
    Maybe there is no safe solution.
    Correct.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Sep 2002
    Posts
    1,747
    I thought c99 had added a boolean in its little underscore frenzy, something like _Boolean or something like that. I could be wrong, and I don't have any c related books dating very far into the 90s, but in the recesses of my mind I remember seeing that...
    */*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/

    "It's hard to believe in something you don't understand." -- the sidhi X-files episode

    galathaea: prankster, fablist, magician, liar

  4. #4
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    I read something by, I think, Herb Sutter a while back on the subject of C99's "bool": it's not a real type (i.e. they haven't added a new fundamental type to the language) - I think it's just a typedef.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  5. #5
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Are you using 'bool' or 'BOOL'

    Which data type you are going to use BOOL or bool?

    If it is bool its is simply a one byte data and is equivalent to unsigned char or unsigned __int8

    It it is BOOL it is just defined as:
    #define BOOL int
    and nothing else.

    So depending on your usage use unsigned xxx or int in your structure definition for safety meeting the same data sizes in both languages.

  6. #6
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: Are you using 'bool' or 'BOOL'

    Originally posted by Ajay Vijay
    Which data type you are going to use BOOL or bool?

    If it is bool its is simply a one byte data and is equivalent to unsigned char or unsigned __int8

    It it is BOOL it is just defined as:
    #define BOOL int
    and nothing else.

    So depending on your usage use unsigned xxx or int in your structure definition for safety meeting the same data sizes in both languages.
    See Paul MacKenzie's reply above: you cannot say that bool is equivalent to unsigned char - it's implementation-defined.

    BOOL is a Windows-thing, and should be avoided by all right-thinking people whenever possible.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Are you using 'bool' or 'BOOL'

    Originally posted by Ajay Vijay
    If it is bool its is simply a one byte data and is equivalent to unsigned char or unsigned __int8
    See section 5.3.3.1 of the ANSI/ISO spec. You cannot guarantee it is an unsigned char or an __int8. Here is the line from the specification, under the section labeled sizeof:
    Note: in particular, sizeof(bool) and sizeof(wchar_t) are implementation-defined
    Regards,

    Paul McKenzie

  8. #8
    Join Date
    Jun 2002
    Location
    Germany
    Posts
    1,557

    my bool is better than thyne

    Thanks for the great comments and suggestions. I think that an applicable solution can be provided for with the following code snippet below. The language elements of C are used as the lowest common denominator.

    Can anyone please verify that the sizeof(my_struct) will be the same in C++ as well as C?

    Thanks.

    Sincerely, Chris.

    Code:
    typedef int INT32;
    
    typedef enum
    {
      my_FALSE = 0,
      my_TRUE  = 1
    }
    my_bool;
    
    struct my_struct
    {
      INT32   n;
      my_bool b;
    }
    my_struct;
    You're gonna go blind staring into that box all day.

  9. #9
    Join Date
    Jun 2001
    Location
    Switzerland
    Posts
    4,443

    Re: my bool is better than thyne

    Originally posted by dude_1967
    Thanks for the great comments and suggestions. I think that an applicable solution can be provided for with the following code snippet below. The language elements of C are used as the lowest common denominator.

    Can anyone please verify that the sizeof(my_struct) will be the same in C++ as well as C?

    Thanks.

    Sincerely, Chris.

    Code:
    typedef int INT32;
    
    typedef enum
    {
      my_FALSE = 0,
      my_TRUE  = 1
    }
    my_bool;
    
    struct my_struct
    {
      INT32   n;
      my_bool b;
    }
    my_struct;
    In the general case, you have no guarantee that the sizeof(int) of the C compiler and that of the C++ compiler are the same. Suppose that you want to write a library that can be used both from C and C++. What happens if the C compiler targets a Win16 platform and the C++ compiler a Win32 platform?

    If you want to see how to implement structures that have a well determined size, go to www.twain.org and download the twain.h file. Have a look at it, it is very instructive.
    Gabriel, CodeGuru moderator

    Forever trusting who we are
    And nothing else matters
    - Metallica

    Learn about the advantages of std::vector.

  10. #10
    Join Date
    Jun 2002
    Location
    Germany
    Posts
    1,557
    Thanks Gabriel,

    Came to the same conclusion shortly after writing the post. The use of enum is not safe over multiple compiler platforms. Thanks for the link. I'll take a look.

    By the way, does anyone know if sizeof(enum) = sizeof(int) is specified?

    Thanks again, everyone. This was a very informative post.

    Sincerely, Chris.

    You're gonna go blind staring into that box all day.

  11. #11
    Join Date
    Jun 2001
    Location
    Switzerland
    Posts
    4,443
    From ANSI/ISO C++, C 1.5.7.2.6:

    Change: In C++, the type of an enumerator is its enumeration. In C, the type of an enumerator is int.
    Example:

    Code:
    enum e { A };
    sizeof(A) == sizeof(int) // in C
    sizeof(A) == sizeof(e) // in C++
    /* and sizeof(int) is not necessary equal to sizeof(e) */
    Gabriel, CodeGuru moderator

    Forever trusting who we are
    And nothing else matters
    - Metallica

    Learn about the advantages of std::vector.

  12. #12
    Join Date
    Sep 2002
    Posts
    1,747
    Just to add to Gabriel's post, instances of one enum type are not even guaranteed to be of the same size as instances of other enum types! Its a weird c++ism which allows a compiler to optimize for size or speed or whatever as long as the enum type has instances large enough to hold its largest value...
    */*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/

    "It's hard to believe in something you don't understand." -- the sidhi X-files episode

    galathaea: prankster, fablist, magician, liar

  13. #13
    Join Date
    Mar 2002
    Location
    Israel
    Posts
    187
    I think there is a way to make bool of the same size in C and C++
    Code:
    #define bool Boolean
    typedef char Boolean;
    (ugly, but possible )
    In the future computer's weight will not exceed 1.5 ton.
    (Popular Mechanics, 1949)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured