Re: why is a bool 4 bytes in g++?

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.
 
D

David Hall

If I recall correctly, bool is implemented as enum bool {false =0,
true =1}; and enums, by default, are of size int, which is 4 bytes
normally.
 
J

Jakob Bieling

David Hall said:
If I recall correctly, bool is implemented as enum bool {false =0,
true =1}; and enums, by default, are of size int, which is 4 bytes
normally.

In C++ bool is a built-in type. No enum needed.
 
R

Ron Natalie

David Hall said:
If I recall correctly, bool is implemented as enum bool {false =0,
true =1}; and enums, by default, are of size int, which is 4 bytes
normally.

You don't recall correctly. bool is a fundemental type in C++ (as is
_Bool in C99). Further, enums are not, by default the sizeof int. The
rule just says they won't be bigger than int unless you have enumerators
outside the range that int can support. Further, it's a bad assumption
that sizeof(int) == 4.
 
D

David Hall

Ron Natalie said:
You don't recall correctly. bool is a fundemental type in C++ (as is
_Bool in C99). Further, enums are not, by default the sizeof int. The
rule just says they won't be bigger than int unless you have enumerators
outside the range that int can support. Further, it's a bad assumption
that sizeof(int) == 4.

Well then, I stand corrected. I just remember something somewhere
about implementation being different from specification. And my
enumerated types always seem to be sizeof(int),* though I haven't
tried for that many enumerated types before.

*As to sizeof(int), "normally" means well, normally, or rather the
type of system one finds on desktops and the one that he is probably
using. *ducks from objects flung by embedded and 64-bit programmers*
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top