R
Roel Schroeven
Tim said:Sorry to ask a question that I'm sure has been answered, but I had trouble
finding it. Why would a bool not be 1 bit, or at most 1 byte? Is it for
reasons of instruction efficiency?
In g++ 2.95.2, a bool is 4 bytes and a char is 1 byte. I have to store a
90,000 entry 2D array of bools. Would it not make more sense for me to
store chars instead of bools, saving me 270,000 bytes?
Not here though:
$ for cpp in /usr/bin/g++-* ; do $cpp sizeof.cpp -o sizeof-$(basename $cpp) ; done
$ for s in sizeof-* ; do echo $s; ./$s; done
sizeof-g++-2.95
bool: 1 byte(s)
sizeof-g++-3.0
bool: 1 byte(s)
sizeof-g++-3.2
bool: 1 byte(s)
sizeof-g++-3.3
bool: 1 byte(s)
(2.95 is 2.95.4 here, but it would surprise there would be a difference
between 2.95.2 and 2.95.4 in that respect)
I suppose consecutive elements are aligned at 4-byte boundaries though,
for efficiency reasons: it's most efficient for a processor to operate
at its native word length, which is 4 bytes for 386-style processors.
See the other responses for alternative ways to handle large numbers of
bools.