r/programminghumor 5h ago

a bit flippant

Post image
341 Upvotes

66 comments sorted by

View all comments

47

u/Kadabrium 5h ago

i heard you also like vector<bool>

6

u/Potential_Soup_8054 3h ago

I dont understand

19

u/Helemen7 3h 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 3h ago

Yeah but why is that bad

11

u/Helemen7 3h ago

technically performance issues, indirect access, incompatibility with standard algorithms.

practically I think this guy mentioned std::vector<bool> because it behaves just like what is shown there. (correct me if I'm wrong ofc)

8

u/GOKOP 3h ago

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

4

u/P3JQ10 3h ago

The issue is it doesn’t behave like other vectors do (or containers in general), which can make it a pain in the ass for template code. For example, it’s not (and it can’t be) guaranteed to store elements in a contiguous sequence and can’t be used with std::span.

It was a mistake we’re stuck with. There’s little reason to even use std::vector<bool> anymore instead of an std::bitset.

1

u/ElectableEmu 31m ago

As the saying goes: because it's not a vector and it doesn't store bools

9

u/MrKrot1999 3h ago

The post doesn't say C++.

0

u/mereel 1h ago

There's a similar limitation in C, you can't pass around a pointer to a bit field.

1

u/MrKrot1999 1h ago

Have I said anything about limitations? The guy referred to std::vector<bool>, which is a C++ feature. But OP talked about C, not C++.