How a bool type variable is stored in memory? (C++)

后端 未结 3 2065
不知归路
不知归路 2021-01-18 05:44

bool test;

sizeof(test) = 1 if using VS 2010. Since every C++ data type must be addressable, the \"test\" bool variable is 8-bits(1 byte).

My qu

3条回答
  •  佛祖请我去吃肉
    2021-01-18 05:53

    Every element of test1 must be addressable. This implies that test1 takes at least 32 bytes (and not bits).

    If you want multiple boolean values to be stored in a single variable, use std::bitset or std::vector (but be aware that the latter is not really a vector of bools, it is a specialization designed to save space).

    IIRC, C++11 also defines std::dynamic_bitset.

提交回复
热议问题