3

I was reading C++ Primer and came across a table containing the minimum guaranteed size of data types of C++, in that it was written that the minimum size of bool is not guaranteed.
Is there any specific reason for it?

6
  • 2
    It is not a duplicate, I am not asking why is the size of boolean 1 byte, I am just asking why isn't there a minimum size of a boolean. Commented Jan 21, 2014 at 6:47
  • All data types are at least one byte. Commented Jan 21, 2014 at 7:22
  • @KeithThompson All data types are 1 byte is automatically guaranteed, because the smallest addressable unit of memory is 1 byte. Commented Jan 21, 2014 at 7:34
  • @ps06756: Which is sufficient for bool, so why bother stating that minimum size again? Commented Jan 21, 2014 at 8:43
  • @MSalters Because, if the book is not mentioning the minimum size of bool, then there is a reason for it. Commented Jan 21, 2014 at 8:56

1 Answer 1

2

Theoretically, a boolean's size need only be a single bit but in memory allocation, types need to be aligned in a way to be compatible with other types and filler bitsare sometimes used on different compilers and computer architectures so that the smallest capsule to store any data type is a byte.

Why is a boolean 1 byte and not 1 bit of size?

As per the last comment, I should add this quote from the Wikipedia article on byte:

The byte /ˈbaɪt/ is a unit of digital information in computing and telecommunications that most commonly consists of eight bits. Historically, the byte was the number of bits used to encode a single character of text in a computer[1][2] and for this reason it is the smallest addressable unit of memory in many computer architectures.

Sign up to request clarification or add additional context in comments.

3 Comments

Don't forget that a variable of type bool also have to be addressable (using e.g. the address-of operator &) which also means it has to be at least the smallest addressable unit of the system, which usually is a byte.
In C++, "byte" is synonymous with "char" and is defined as the smallest addressable unit of memory.
You should quote relevant parts of the C++ and/or C standard, because I cannot find anything in the former that guarantees one byte for a bool. The question is why the minimum size it not guaranteed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.