0
  • When the lowest possible allocable in memory is 1 byte, how is a boolean (1-bit) stored in memory?
  • If C++ uses 1 byte for each boolean, why don't use the 7 empty bits for other booleans?
Jibel
  • 127
  • 2
  • 2
  • 15
  • 2
    A boolean is stored in 1 whole byte. Yes you waste 7 bits, but you can't have addresses of single bits in memory. Possible [duplicate](https://stackoverflow.com/q/19351483/20785822). – Joel May 15 '23 at 07:34
  • So why C++ doesn't use the waste of 7 bits for other booleans? – Jibel May 15 '23 at 07:36
  • `bool` does have a smaller range, it can take 2 different values. Not impossible – 463035818_is_not_an_ai May 15 '23 at 07:36
  • 2
    ... Except if you use the infamous `std::vector`, which actually uses bit manipulation to store up to 8 boolean values in each byte. – Lukas-T May 15 '23 at 07:37
  • You can use `std::bitset` or create your own container with which you can save some memory. – Joel May 15 '23 at 07:37
  • 1
    you already state the answer "...even if we want to do this, it is pointless because the smallest addressable value in the memory is 1 byte equal to 8 bits." not much more to say – 463035818_is_not_an_ai May 15 '23 at 07:37
  • @Jibel Because then two Boolean variables would have the same address (being stored in the same byte). Imagine the code `bool a, b; bool *ptr = &a; *ptr = true;` Which variable is assigned to, a, b, both? – john May 15 '23 at 07:39
  • 2
    if you are interested in the topic i suggest you to read about `std::vector` it is an attempt to use less than 1 byte for a single `bool`, but references and pointers to elements are problematic. In hindsight its a mistake, (though the main issue is that it is a `std::vector`, I suppose it would be much less problematic if it had a different name) – 463035818_is_not_an_ai May 15 '23 at 07:40
  • 3
    Howard Hinnant's essay on vector is very good: https://howardhinnant.github.io/onvectorbool.html Also, Scott Meyer has talked about proxy classes in item 6 of "Effective Modern C++". – Hari May 15 '23 at 07:43
  • 1
    Does this answer your question? [How is a bool represented in memory?](https://stackoverflow.com/questions/19351483/how-is-a-bool-represented-in-memory) – M. Yousfi May 15 '23 at 07:44
  • @Jibel taht would not worth the effort. To pack several bits in a single byte (or anything else) you would have to manipulate the data with some bit-level operators that would cost time for almost no gain. – Jean-Baptiste Yunès May 15 '23 at 07:45
  • 1
    Why doesn't C++ pack 7 more `bool`s into the other bits? Because it's very important that every C++ object (including a `bool`) have its own address, and be thread-safe to access. See [C++ memory model and race conditions on char arrays](https://stackoverflow.com/q/19903338) for discussion of the relevant guarantees, e.g. why a word-addressable machine couldn't have 8-bit `char`, for the same reason. (All modern mainstream CPUs are byte-addressable; only some DSPs are word addressable and have `CHAR_BIT` = 16 or 24 or whatever.) – Peter Cordes May 15 '23 at 07:47
  • its a trade-off. Nowadays memory is cheap while cpu speed is hard to get. In a reverse world, it would make sense to pack 8 bools in a byte – 463035818_is_not_an_ai May 15 '23 at 07:48
  • @Joel Using std::bitset does not help. It allocates 1 byte like a boolean. – Jibel May 15 '23 at 09:48
  • @MustaphaYousfi No. – Jibel May 15 '23 at 09:52
  • @Jean-Baptiste Yunès Yes, I agree with this, as 463035818_is_not_a_number pointed out: *Nowadays memory is cheap while cpu speed is hard to get. In a reverse world, it would make sense to pack 8 bools in a byte* – Jibel May 15 '23 at 09:56
  • @Jibel If you have a `std::bitset<1>` then yes, you are wasting at least 7 bits of memory again, assuming it allocates 1 byte. However, realistically it might even allocate 8 bytes. So yes, if you have only a few bits, maybe don't use a `std::bitset`. But it still stores each Boolean in 1 single bit. – Joel May 15 '23 at 11:13

0 Answers0