I\'m reading The C++ Programming Language. In it Stroustrup states that sizeof(char) == 1
and 1 <= sizeof(bool)
. The specifics depend
A byte is the smallest addressable unit of memory.
Consider the following code:
bool b[9];
bool *pb0 = &b[0];
bool *pb1 = &b[1];
for (int counter=0; counter<9; ++counter)
{
// some code here to fill b with values
b[counter] = true;
}
If bool is stored as 1 bit, then pb0 will equal pb1, because both have the same address. This is clearly not desirable!
Additionally the assignment in the loop will result in non-trival assembly code. It will involve a different bit shift in each iteration of the loop. In high-performance software, those extra bit-shift operations can slow down the application needlessly.
The STL library provides a work-around in situations where space DO matter. The use of std::vector<bool> will store bool as 1 bit. The paradox of the example above do not apply because