By 0xbadf00d

2011-02-04 12:15:30 8 Comments

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?


@GManNickG 2011-02-04 12:16:52

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.

@Eagle 2011-05-30 09:29:51

is there a flag that i need to compile my program with, that my compiler will use only 1 byte for bool?

@GManNickG 2011-05-30 10:02:12

@Eagle: That's up to your compiler, I'm not sure. It's probably best you left it up to your compiler.

@user3063349 2016-02-10 16:40:56

note that std::vector<bool> is optimized to a vector containing 1bit bools by the standard.

@Yi Ling 2011-08-04 01:50:49

"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."

@Christopher Creutzig 2013-12-12 22:27:20

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?

@kinokijuf 2014-05-24 20:50:24

@ChristopherCreutzig It is a proof by counterexample.

@0xbadf00d 2016-05-13 15:43:35

@kinokijuf It's not a counterexample. There are many things in Visual C++ that are not standard-compliant.

@BЈовић 2011-02-04 12:19:45

See 5.3.3 paragraph 1 :

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

@peoro 2011-02-04 12:16:49

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

@user3063349 2016-02-10 16:39:45

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.

@paulm 2016-07-01 11:41:57

1 means 8 bits or 1 byte in the standard

@peoro 2016-07-02 02:13:17

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.

Related Questions

Sponsored Content

25 Answered Questions

[SOLVED] What is the "-->" operator in C++?

24 Answered Questions

[SOLVED] What does the C++ standard state the size of int, long type to be?

  • 2009-02-26 07:59:23
  • Jérôme
  • 1230171 View
  • 699 Score
  • 24 Answer
  • Tags:   c++ c++-faq

1 Answered Questions

[SOLVED] The Definitive C++ Book Guide and List

  • 2008-12-23 05:23:56
  • grepsedawk
  • 2387487 View
  • 4243 Score
  • 1 Answer
  • Tags:   c++ c++-faq

26 Answered Questions

[SOLVED] Why do we need virtual functions in C++?

40 Answered Questions

18 Answered Questions

[SOLVED] Why should C++ programmers minimize use of 'new'?

5 Answered Questions

19 Answered Questions

4 Answered Questions

Sponsored Content