Thread: Confused about an uninitialized bool variable.

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    21

    Confused about an uninitialized bool variable.

    Hello.

    Sparing you the details of how I have come across this issue I am just going to paste an example which I simply do not get (although it's probably easy to explain - knowing my luck it has something to do with undefined behaviour

    Code:
    #include <stdio.h>
    #include <stdbool.h>
    
    int main(void)
    {
        bool b;
    
        if (b)
            printf("%d: True\n", b);
        else
            printf("%d: False\n", b);
    
        if (!b)
            printf("%d: False\n", b);
        else
            printf("%d: True\n", b);
    
        return 0;
    }
    Running the program results in:

    Code:
    assiduus@ubuntu:~$ ./a.out
    183: True
    183: False
    When I manually assign this random value to b, 1 is stored instead, which I understand is a normal behaviour when you try to store a nonzero value into b. The result is what you would expect:

    Code:
    assiduus@ubuntu:~$ ./a.out
    1: True
    1: True
    How come the same thing does not happen during the first (b uninitialized) run? And how is it that 183 is true or false depending on how you form the "question"?

    Cheers,

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Very strange. b is undefined but that's beside the point.
    You should always get True,True or False, False.
    Unless it's some strange dialect of C and 'bool' data type is indeed some unique thing that doesn't behave like int.
    Last edited by nonoob; 02-21-2011 at 02:16 PM.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,631
    Please see this link: bool

    Jim

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    There is no requirement that _Bool only be capable of storing 0 and 1; it must simply be able to hold both those values.

    As you imply, converting a scalar to _Bool requires that the value be converted to 0 or 1, but this is only on conversion to _Bool. So _Bool is probably implemented as a char, with special code to handle assignment/conversion to _Bool. However, _Bool is likely capable of holding larger values.

    It's possible, on the other hand, that if any bits other than the LSB are set in a _Bool, the implementation considers it a trap representation, and it'd be undefined to read from that.

  5. #5
    Registered User
    Join Date
    Jul 2010
    Posts
    21
    Thanks for all the answers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers
    By MrMatt027 in forum C++ Programming
    Replies: 14
    Last Post: 12-10-2010, 04:32 PM
  2. A member variable that can be of any type.
    By g4j31a5 in forum C++ Programming
    Replies: 3
    Last Post: 10-13-2010, 06:15 AM
  3. Quick: Alternative to using global variable
    By abrownin in forum C++ Programming
    Replies: 1
    Last Post: 05-02-2010, 04:25 AM
  4. passing params between managed c++ and unmanaged c++
    By cechen in forum C++ Programming
    Replies: 11
    Last Post: 02-03-2009, 08:46 AM
  5. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM