134

I can't find an answer in the standard documentation. Does the C++ language standard require sizeof(bool) to always be 1 (for 1 byte), or is this size implementation-defined?

einpoklum
  • 118,144
  • 57
  • 340
  • 684
0xbadf00d
  • 17,405
  • 15
  • 67
  • 107
  • If your code depends on `bool` to be 1 byte, you can check it at compile-time: `static_assert(sizeof(bool) == 1, "OMG bool is big here");`. – nielsen Jun 29 '23 at 06:49

5 Answers5

193

sizeof(bool) is implementation defined, and the standard puts notable emphasis on this fact.

§5.3.3/1, abridged:

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

Footnote 69):

sizeof(bool) is not required to be 1.

GManNickG
  • 494,350
  • 52
  • 494
  • 543
20

http://msdn.microsoft.com/en-us/library/tf4dy80a.aspx

"In Visual C++4.2, the Standard C++ header files contained a typedef that equated bool with int. In Visual C++ 5.0 and later, bool is implemented as a built-in type with a size of 1 byte. That means that for Visual C++ 4.2, a call of sizeof(bool) yields 4, while in Visual C++ 5.0 and later, the same call yields 1. This can cause memory corruption problems if you have defined structure members of type bool in Visual C++ 4.2 and are mixing object files (OBJ) and/or DLLs built with the 4.2 and 5.0 or later compilers."

Yi Ling
  • 277
  • 3
  • 11
  • 2
    You do realize that this is somewhat related to the question, but clearly not an answer, since 0xbadf00d asked about the standard, not some specific/arbitrarily selected compiler implementation, right? – Christopher Creutzig Dec 12 '13 at 22:27
  • 16
    @ChristopherCreutzig It is a proof by counterexample. – kinokijuf May 24 '14 at 20:50
  • 19
    @kinokijuf It's not a counterexample. There are many things in Visual C++ that are not standard-compliant. – 0xbadf00d May 13 '16 at 15:43
12

It's implementation defined. Only sizeof(char) is 1 by the standard.

peoro
  • 25,562
  • 20
  • 98
  • 150
  • 3
    pls note that the 1 in the standard can mean 4 byte. Than every type is a product of 4. So care that the standard ONLY defines char is the 1, but not defines the measurment. – user3063349 Feb 10 '16 at 16:39
  • 1 means 8 bits or 1 byte in the standard – paulm Jul 01 '16 at 11:41
  • 18
    1 byte. The number of bits per byte is not defined by the standard (it needs to be at least 8 IIRC), but can be found in `CHAR_BIT`, defined in `climits`. – peoro Jul 02 '16 at 02:13
3

See 5.3.3 paragraph 1 :

[Note: in particular, sizeof(bool) and sizeof(wchar_t) are implementation-defined.69) ]

BЈовић
  • 62,405
  • 41
  • 173
  • 273
-5

Boolean occupy 8bit in a memomry. As per the recent development

  • 1
    Please cite your source for this. Current C++ draft does not indicate what you state. – Mat Jan 08 '23 at 09:41
  • 1
    As other answers have correctly noted, and @Mat has implied, this is clearly false. Just because the implementation *you* used uses an 8-bit section of memory, does not mean that is always the case. I know for a fact that a custom compiler we use, uses a 4-bit "microword" for booleans on their microprocessor. – SimonC Jan 08 '23 at 18:37