Thread: Unsigned : 1; or bool ? C

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    16

    Unsigned : 1; or bool ? C

    I want to know which one uses less memory, I tried looking for it but didn't get a accurate answer.
    Say I have a thousand instances of a C struct, which of the following would be better to sign a state?
    Code:
    unsigned myvar : 1;
    or
    Code:
    bool myvar;
    Thank you for your time.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Find out for yourself: sizeof( bool ) or sizeof( unsinged char )

    Actually I see you are using a bit field. In either case, you still won't end up smaller than one byte.


    Quzah.
    Last edited by quzah; 06-23-2011 at 04:46 PM.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    16
    Ah I see.
    Thank you for your time.

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    'unsigned' without a type is equivalent to 'unsigned int'
    Even if you only declare 1 bit of a bit field it still takes up sizeof(underlying type) bytes. In your case sizeof(unsigned int).
    The main purpose* of a bit field is to pack multiple smaller variables in to a bigger "container-like" type. For example, you could fit 8 "bool-type" variables in to a single byte to save space or to simplify access to individual bits.

    *) as far as I know, there might be other uses I am oblivious to

    Edit: Also, isn't bool a C++ type? gcc says "error: ‘bool’ undeclared (first use in this function)" even with --std=c99
    Edit2: Apparently it's called _Bool in gcc, and it's valid for all the C standards.. o_O
    Last edited by _Mike; 06-23-2011 at 05:14 PM.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    It's in C99, but you've got to include the magic header which is <stdbool.h>

  6. #6
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by tabstop View Post
    It's in C99, but you've got to include the magic header which is <stdbool.h>
    Thank you.
    printf("%d\n", sizeof(bool)); gives a weird warning when compiling with -Wall:
    test.c:16:2: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘long unsigned int’
    So the compiler thinks bool is an long unsigned int, but sizeof(bool) says 1.. hmm, interesting

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by _Mike View Post
    Thank you.
    printf("%d\n", sizeof(bool)); gives a weird warning when compiling with -Wall:
    test.c:16:2: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘long unsigned int’
    So the compiler thinks bool is an long unsigned int, but sizeof(bool) says 1.. hmm, interesting
    No. What sizeof produces is a "long unsigned int" in your case. Not what type bool is.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Actually no mike.... printf("%d\n",sizeof(bool)) will see the return type of sizeof()... not the type of bool.

    printf("%u\n",sizeof(bool)); should print 1.

  9. #9
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Haha yeah, just ignore my last post. I just realized my mistake and came back to edit it before someone got a chance to see it. I say stupid things without thinking far to often

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Mistakes are how we learn. Even if we don't learn from them, someone else might.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    Quote Originally Posted by CommonTater View Post
    printf("%u\n",sizeof(bool)); should print 1.
    The size_t format spec in C99 is %zu. Doesn't matter here but if you're printing multiple things and sizeof(size_t) != sizeof(unsigned) weird things'll happen.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    Mistakes are how we learn. Even if we don't learn from them, someone else might.
    Quzah.
    Absolutely... I made a mistake once, in 1977... and I'll never forget it!

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by adeyblue View Post
    The size_t format spec in C99 is %zu. Doesn't matter here but if you're printing multiple things and sizeof(size_t) != sizeof(unsigned) weird things'll happen.
    Interresting... I just looked that up, and it's duly filed under "Ya learns something new everyday"...

  14. #14
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by CommonTater View Post
    Absolutely... I made a mistake once, in 1977... and I'll never forget it!
    This looks like a place for a quote

    Quote Originally Posted by Eleanor Roosevelt
    Learn from the mistakes of others. You can’t live long enough to make them all yourself.

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by stahta01 View Post
    This looks like a place for a quote


    Originally Posted by Eleanor Roosevelt
    Learn from the mistakes of others. You can’t live long enough to make them all yourself.
    Or, at the risk of quoting my father...
    "I never make the same mistake twice. I'm always too busy making new ones".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 10-06-2009, 09:37 AM
  2. BOOL bool ? unresolved external symbol
    By xwielder in forum C Programming
    Replies: 6
    Last Post: 05-20-2008, 08:39 AM
  3. How get bool's from std::vector<bool>
    By 6tr6tr in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2008, 05:24 AM
  4. Replies: 7
    Last Post: 02-08-2008, 06:31 PM
  5. Interesting: bool != BOOL
    By lightatdawn in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 01-09-2002, 11:09 AM