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?
Is sizeof(bool) defined in the C++ language standard?
115.8k views Asked by 0xbadf00d At
4
There are 4 answers
3
On
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."
sizeof(bool)
is implementation defined, and the standard puts notable emphasis on this fact.§5.3.3/1, abridged:
Footnote 69):