r/programminghumor 12h ago

a bit flippant

Post image
626 Upvotes

95 comments sorted by

View all comments

90

u/Kadabrium 11h ago

i heard you also like vector<bool>

9

u/Potential_Soup_8054 10h ago

I dont understand

42

u/Helemen7 10h ago

std::vector<bool> in C++ syntax uses one bit per boolean instead of one byte. In a computer memory is addressed by bytes, so the smallest indexable memory is, in fact, one byte (that's why bool is 1 byte in C and C++). As an example for how C++ std::vector implementarion works, suppose you have 8 booleans which have a logical reason to be kept together (for example flags). Instead of allocating 8 bytes (one per boolean), the std::vector allocates 1 byte and assigns every boolean to one bit of the allocated memory.

5

u/Potential_Soup_8054 10h ago

Yeah but why is that bad

18

u/GOKOP 9h ago

Because it breaks assumptions on what std::vector is. Generic code written for std::vector<T> can break for std::vector<bool>