-3

Possible Duplicate:
Is sizeof(bool) implementation defined in C++?

Im writing code that is compiled in vc++ with a g++ lib (libpng) and need to know if bool is the same size and bits in g++ and vc++. All I know is false is all 0's and that sizeof(bool) is 1 in vc++

Community
  • 1
  • 1
Cole Tobin
  • 9,206
  • 15
  • 49
  • 74
  • 4
    If you're writing code to depend on the sizeof something then it's a portability nightmare. The only type guaranteed to be 1 is `char`. – Pubby May 09 '12 at 04:09
  • true, but nowhere is said that that char has to be 8 bits.. that's also fun – graywolf Jun 30 '15 at 13:29
  • @Paladin that's said in POSIX. Too bad some systems aren't POSIX-compliant. – Ruslan Mar 14 '18 at 16:09
  • @Ruslan hm, what is an example of POSIX compliant system? I know that linux doesn't qualify, do *BSD variants fit that definition? – graywolf Mar 14 '18 at 17:02
  • 1
    @Paladin see [POSIX-certified systems](https://en.wikipedia.org/wiki/POSIX#POSIX-certified) list at Wikipedia, as well as "mostly" POSIX-compliant. – Ruslan Mar 14 '18 at 18:30

2 Answers2

3

There are versions of g++ (and versions of Visual C++ as well for that matter) for which on some platforms sizeof(bool) is not equal to 1. So no, you can't assume that it will be the same on g++ and Visual C++. You can't even assume that it's the same on different versions of the same compiler or the same version on different platforms.

sepp2k
  • 363,768
  • 54
  • 674
  • 675
  • So I should just not use two different compilers? – Cole Tobin May 09 '12 at 13:32
  • 3
    @ColeJohnson No, you just shouldn't write code that relies on `bool` (or any other type) having a specific size. – sepp2k May 09 '12 at 13:35
  • so alway use the same compiler and possibly – Cole Tobin May 09 '12 at 13:56
  • 3
    @ColeJohnson No, there's no need to always use the same compiler. If your code doesn't work on all standard-conforming compilers, it's broken. Let me ask you this: Why do you need `sizeof(bool)` to be 1 for your code to work? – sepp2k May 09 '12 at 14:01
  • @ColeJohnson Regarding cstdint: It is perfectly fine to use the types from cstdint to make sure that the values you put into your integers actually fit into it (in fact you definitely should do that where applicable). However the only values you should ever put into a bool variable are true and false and those will always fit into a bool, no matter which compiler you're using and what `sizeof(bool)` is. – sepp2k May 09 '12 at 14:10
  • It does not need to be 1, it just needs to be universal. – Cole Tobin May 09 '12 at 14:42
  • I'm working with booleans. I just need them to be compatible – Cole Tobin May 09 '12 at 20:40
  • 2
    @ColeJohnson That's pretty vague and unspecific. The fact is if you have code that works on a platform where `sizeof(bool)` is one value and breaks on a platform where it is another value, your code is broken. – sepp2k May 10 '12 at 05:51
2

As you can read in another question C++03 Standard $5.3.3/1 says,

sizeof(char), sizeof(signed char) and sizeof(unsigned char) are 1; the result of sizeof applied to any other fundamental type (3.9.1) is implementation-defined. [Note: in particular,sizeof(bool) and sizeof(wchar_t) are implementation-defined.69)

so the result of sizeof(bool) is implementation defined.

Community
  • 1
  • 1
Szilárd Pfeiffer
  • 1,636
  • 1
  • 12
  • 6