3

Shouldn't bool has a minimum size of 1 byte?

Bool has no-specified min size

mooder
  • 341
  • 2
  • 8
  • Maybe that's because it's only technically 1 bit? And everything has a minimum size of one byte (`1 * sizeof(char)`). There's probably a note somewhere – Artyer Jan 28 '20 at 00:38
  • I checked everywhere and nothing. I found a lot of typos also... I'm wondering if that's even a good reference at this point (I'm just at page 64)! – mooder Jan 28 '20 at 00:43
  • 1
    "6 significant digits" isn't a size so it's not really clear what this table is showing – M.M Jan 28 '20 at 01:22
  • Does this answer your question? [What is the minimum size of a boolean?](https://stackoverflow.com/questions/32029213/what-is-the-minimum-size-of-a-boolean) –  Jan 28 '20 at 05:57
  • 1
    So, did you get your answer? – Asteroids With Wings Feb 02 '20 at 18:33
  • Yes thank you Asteroids With Wings, I needed to read a bit more to process your answer completely. – mooder Feb 05 '20 at 19:33

1 Answers1

6

This is a bit of an academic thing.

Though they convert to numbers (and, though, like everything else, they will ultimately be fundamentally represented by numbers in your computer's memory), booleans are not numbers. Your bool can take the value true, or the value false. There's no semantic description of how many "bytes" that takes, even though you do need at least 1 bit of information (in the informatics sense) to store that data.

There's also the associated oddity that the C++ standard gives minimum widths for the integer types, leaving bool out because it's not an integer type. Similarly, the C limits macros don't include one for bool (though this is likely more because bools didn't exist in early C). The book apparently uses this information more or less directly, adding the floating-point types along with their mathematical constraints (notice that these are not given in terms of byte widths either).

However, the standard does go on to concede that a bool "has the same object representation, value representation, and alignment requirements as an implementation-defined unsigned integer type" (ref), because even the standard needs to acknowledge that it exists in our physical reality. Going down that route, then, we don't even need the standard: we know that your bool will need at least one byte, because every object takes up at least a byte ... unless you're packing it into a vector<bool> or bitfield.

If I were writing that book, I'd probably have left a note to this effect at the very least.

Asteroids With Wings
  • 17,071
  • 2
  • 21
  • 35
  • *The character types, **`bool`**, the signed and unsigned integer types, and cv-qualified versions thereof, are collectively termed integral types. A synonym for integral type is integer type.* – rosshjb Dec 16 '21 at 06:18