Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

memory layout of vector<bool>

Can someone please explain the memory layout of the data stored in a vector<bool>?

like what layout does the memory have from address &myVec[0] upwards? Does it depend on endianness? Is the memory continguous for all stored values? (i'm aware that vector<bool> doesn't actually store booleans). can I dump the content of a vector<bool> into a file using memcopy to get a bitmap of my values?

please no questions like "what do you need it for" or suggestions like using bitsets or boost.

Thank you for an accurate explanation

like image 522
Mat Avatar asked Jan 28 '26 13:01

Mat


1 Answers

The std::vector will simply manage a raw array on the heap. So whan you do &myVec[0] you get the address of the first element of this array. As it's an array it...follows the rules of a raw array..

BUT

std::vector is a special case, a specific implementation, a mistake of the C++ commitee that is NOT a vector of bool but a container managing bits. So avoid using this one.

like image 147
Klaim Avatar answered Jan 31 '26 13:01

Klaim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!