Velocity Reviews > C++ > Re: why is a bool 4 bytes in g++?

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

Tim Partridge
Guest
Posts: n/a

 07-18-2003
Actually, the matrix of bools I have to store is an upper triangular
matrix. For anyone who might read this later, I found this site:

which describes the how to convert an upper triangular matrix to a 1D
array. Entry (i,j) in the matrix corresponds to entry (2n-i)(i-1)/2 + j
in the array, where n is the number of rows (or columns), and 1 <= i,j <= n.

On Thu, 17 Jul 2003, Jakob Bieling wrote:

> "Tim Partridge" <(E-Mail Removed)> wrote in message
> news(E-Mail Removed)...
> > 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?

>
>
> If you are after saving space, you should go for a vector< vector
> <bool> >. And if you want to save even more, then just use a single vector
> <bool> and calculate the 2D offsets yourself (not too difficult). Actually,
> I would even prefer the vector <bool> over vector< vector <bool> >.
>
> hth
> --
> jb
>
> (replace y with x if you want to reply by e-mail)
>
>
>

Tim Partridge
Guest
Posts: n/a

 07-21-2003
On Fri, 18 Jul 2003, Tim Partridge wrote:

*snip*
> which describes the how to convert an upper triangular matrix to a 1D
> array. Entry (i,j) in the matrix corresponds to entry (2n-i)(i-1)/2 + j
> in the array, where n is the number of rows (or columns), and 1 <= i,j <= n.

*snip*

Note the additional restriction of i >= j.