quant
quant

Reputation: 23112

How is a bool represented in memory?

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
};

Upvotes: 22

Views: 29582

Answers (4)

Atle
Atle

Reputation: 1877

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

Upvotes: 5

LihO
LihO

Reputation: 42133

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

Upvotes: 23

user2707175
user2707175

Reputation: 1137

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.

Upvotes: -2

Matteo Italia
Matteo Italia

Reputation: 126927

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

Upvotes: 6

Related Questions