21

As already discussed in the docs, a bool data type occupies at least a byte of memory. A similar question was asked on SO before (How a bool type variable is stored in memory? (C++)), but this discussion and the documentation only seem to discuss the amount of space occupied by a boolean data type, not what actually happens in memory when I do this:

bool b = true;

So what does actually happen in memory? What happens to the 7 bits that are not used in storing this information? Does the standard prescribe behavior for this?

Are they undefined? Or did someone at C++ headquarters just do this:

enum bool : char 
{
  false = 0,
  true = 1
};
Community
  • 1
  • 1
quant
  • 21,507
  • 32
  • 115
  • 211
  • possible duplicate of [How a bool type variable is stored in memory? (C++)](http://stackoverflow.com/questions/7967924/how-a-bool-type-variable-is-stored-in-memory-c) – Daniel A. White Oct 13 '13 at 23:28
  • 17
    [OT]The notion of "C++ headquarters" made me laugh; it strikes me as a gray place with dusty copies of the standard all around and hundreds of language lawyers questioning over STL vs stdlib and similar diatribes.[/OT] – Matteo Italia Oct 13 '13 at 23:30
  • As an extension to this question, would I be better off using char (single byte) as a opposed to a bool? – Shifu Nov 24 '21 at 07:55
  • Related: [Does the C++ standard allow for an uninitialized bool to crash a program?](https://stackoverflow.com/q/54120862) - some ABIs do define the object-representation for `bool` that way, so compilers are allowed to assume that it won't ever read some other other bit-pattern (like `42`) from a `bool`. Thus if it does, it's UB. – Peter Cordes May 15 '23 at 07:53

4 Answers4

21

Standard states that bool values behave as integral types, yet it doesn't specify their concrete representation in memory:

"Values of type bool are either true or false. As described below, bool values behave as integral types. Values of type bool participate in integral promotions" ~ C++03 3.9.1 §6

"A synonym for integral type is integer type. The representations of integral types shall define values by use of a pure binary numeration system" ~ C++03 3.9.1 §7

Yet it defines the integral promotion from bool to int:

"An rvalue of type bool can be converted to an rvalue of type int, with false becoming zero and true becoming one. These conversions are called integral promotions." ~ C++03 4.5 §4-5

as well as conversion from other types to bool:

"A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true." ~ C++03 4.12 §1

LihO
  • 41,190
  • 11
  • 99
  • 167
  • 2
    C++14 specifies that `bool` *is* an integral type and uses a pure binary numeration system, although it still doesn't say exactly what that means. We might expect `false` to have the same representation as `0`, and `true` to have the representation of `1` but it does not actually say that. – M.M Oct 28 '15 at 00:26
6

The standard doesn't mandate anything for the binary representation of bools; it just says that, when converting to other integral types, a true bool will become 1 and a false bool will become 0.

This of course suggests an implementation similar in spirit to the one you said, where such conversions would become essentially no-ops or plain integer widening (but remember that bool is mandated to be a primitive type, not an enumeration type).

Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
4

You can test things like this by copying the memory, ignoring which type it is. This program reads the raw memory value of test_bool and puts it into test_int so that you may print it.

int test_int = 0;
bool test_bool;

test_bool = true;
memcpy (&test_int, &test_bool, sizeof(bool));
printf ("True value is: %d\n", test_int);

test_bool = false;
memcpy (&test_int, &test_bool, sizeof(bool));
printf ("False value is: %d\n", test_int);

For me, this program gives:

True value is: 1
False value is: 0
Atle
  • 1,867
  • 12
  • 10
  • 2
    It would be more meaningful to copy the `bool` to a `char` (or better, to a `char[sizeof(bool)]`), otherwise the interpretation of this half-overwritten `int` may be complicated by other issues (on a 32-bit big-endian machine with `true` stored as a single-byte 1, for example, your code would output 16777216). – Matteo Italia Oct 14 '13 at 00:25
  • Yes, probably best to use a type of the same size as bool. – Atle Oct 14 '13 at 00:41
-1

I didn't program in C++ since a couple of months but AFAIR the rule is the following: 0 - false; any value different than 0 - true; (by default it's 1 but AFAIR if you convert it from other integer value, ex. 2 will be also treated as true).

Thus you can say that C++ in some way waste memory (others too) as it could use one bit only but it's simpler to make compiler then.

In C++ you can also define bit fields (bit fields in wikipedia) but it's not used often.

user2707175
  • 1,133
  • 2
  • 17
  • 44
  • 3
    Careful - what the `bool` type stores and what is the notion of `true` for a conditional instruction is different stuff... – Matteo Italia Oct 13 '13 at 23:36