 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
stephan beal Guest
|
Posted: Thu Jul 17, 2003 4:25 pm Post subject: Re: why is a bool 4 bytes in g++? |
|
|
Jakob Bieling wrote:
| Quote: | 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 .
|
Forgive my ignorance, but why would a vector<bool> use less space than
(e.g.) an array of bool? The bool members are the same size, either way
(unless there's a specialization for vector<bool>, i suppose, which
accomodates them in bits).
i'll admit that vector is generally a better choice, i'm just curious why
you say it would take less space.
--
----- stephan beal
Registered Linux User #71917 http://counter.li.org
I speak for myself, not my employer. Contents may
be hot. Slippery when wet. Reading disclaimers makes
you go blind. Writing them is worse. You have been Warned.
|
|
| Back to top |
|
 |
Ron Natalie Guest
|
Posted: Thu Jul 17, 2003 4:32 pm Post subject: Re: why is a bool 4 bytes in g++? |
|
|
"stephan beal" <stephan (AT) wanderinghorse (DOT) net> wrote
| Quote: | Jakob Bieling wrote:
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 .
Forgive my ignorance, but why would a vector<bool> use less space than
(e.g.) an array of bool?
|
Because vector<bool> is a specializtion of vector. Rather than just making
an array of bool types like vector does normally, it packs them together.
Of course, some of the vector semantics behave oddly as a result.
|
|
| Back to top |
|
 |
Jakob Bieling Guest
|
Posted: Thu Jul 17, 2003 4:42 pm Post subject: Re: why is a bool 4 bytes in g++? |
|
|
"stephan beal" <stephan (AT) wanderinghorse (DOT) net> wrote
| Quote: | Jakob Bieling wrote:
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 .
Forgive my ignorance, but why would a vector<bool> use less space than
(e.g.) an array of bool? The bool members are the same size, either way
(unless there's a specialization for vector<bool>, i suppose, which
accomodates them in bits).
i'll admit that vector is generally a better choice, i'm just curious why
you say it would take less space.
|
"The vector<bool> Class is a full specialization of the template class
vector for elements of type bool with an allocator for the underlying type
used by the specialization.
"The vector<bool> reference Class is a nested class whose objects are
able to provide references to elements (single bits) within a vector<bool>
object."
This is an excerpt from the documentation of the implementation I use.
There is also info about this in the Standard (23.2.5).
In other words, there is a specialization for bool that uses the space
efficiently, like you guessed already.
regards
--
jb
(replace y with x if you want to reply by e-mail)
|
|
| Back to top |
|
 |
Roel Schroeven Guest
|
Posted: Thu Jul 17, 2003 5:17 pm Post subject: Re: why is a bool 4 bytes in g++? |
|
|
Tim Partridge wrote:
| Quote: | 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.
--
"Codito ergo sum"
Roel Schroeven
|
|
| Back to top |
|
 |
David Hall Guest
|
Posted: Thu Jul 17, 2003 9:04 pm Post subject: Re: why is a bool 4 bytes in g++? |
|
|
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.
|
|
| Back to top |
|
 |
Jakob Bieling Guest
|
Posted: Thu Jul 17, 2003 9:12 pm Post subject: Re: why is a bool 4 bytes in g++? |
|
|
"David Hall" <dlwh (AT) aol (DOT) com> wrote
| Quote: | 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.
--
jb
(replace y with x if you want to reply by e-mail)
|
|
| Back to top |
|
 |
Ron Natalie Guest
|
Posted: Thu Jul 17, 2003 9:29 pm Post subject: Re: why is a bool 4 bytes in g++? |
|
|
"David Hall" <dlwh (AT) aol (DOT) com> wrote
| Quote: | 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.
|
|
| Back to top |
|
 |
E. Robert Tisdale Guest
|
Posted: Thu Jul 17, 2003 10:08 pm Post subject: Re: why is a bool 4 bytes in g++? |
|
|
Tim Partridge wrote:
| Quote: | 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?
cat bool.cc
#include |
int main(int argc, char* argv[]) {
bool array[33];
std::cout << sizeof(bool) << " = sizeof(bool)" << std::endl;
std::cout << sizeof array << " = sizeof array" << std::endl;
return 0;
}
| Quote: | g++ -Wall -ansi -pedantic -O2 -o bool bool.cc
./bool
1 = sizeof(bool) |
33 = sizeof array
| Quote: | g++ --version
g++ (GCC) 3.2 20020903 (Red Hat Linux 8.0 3.2-7) |
You should upgrade your compiler.
|
|
| Back to top |
|
 |
David Hall Guest
|
Posted: Fri Jul 18, 2003 1:16 am Post subject: Re: why is a bool 4 bytes in g++? |
|
|
"Ron Natalie" <ron (AT) sensor (DOT) com> wrote
| Quote: | "David Hall" <dlwh (AT) aol (DOT) com> wrote
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.
|
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*
|
|
| Back to top |
|
 |
Evan Guest
|
Posted: Fri Jul 18, 2003 2:11 am Post subject: Re: why is a bool 4 bytes in g++? |
|
|
stephan beal <stephan (AT) wanderinghorse (DOT) net> wrote
| Quote: | Jakob Bieling wrote:
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 .
Forgive my ignorance, but why would a vector<bool> use less space than
(e.g.) an array of bool? The bool members are the same size, either way
(unless there's a specialization for vector<bool>, i suppose, which
accomodates them in bits).
i'll admit that vector is generally a better choice, i'm just curious why
you say it would take less space.
|
The other replies to this are only half right... Yes, vector<bool> is
a specialization of vector<T> that the standard created in order to
make it possible to pack, say, 8 bools into one byte. However, the
standard does not require this to be done, and at least accoring to
some resources (e.g. More Exceptional C++ by Sutter, a compilation of
expanded versions of his Guru of the Week) many implementations do not
make this optimization.
|
|
| Back to top |
|
 |
Jakob Bieling Guest
|
Posted: Fri Jul 18, 2003 8:09 am Post subject: Re: why is a bool 4 bytes in g++? |
|
|
"Evan" <eed132 (AT) psu (DOT) edu> wrote
| Quote: | stephan beal <stephan (AT) wanderinghorse (DOT) net> wrote
Jakob Bieling wrote:
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 .
Forgive my ignorance, but why would a vector<bool> use less space than
(e.g.) an array of bool? The bool members are the same size, either way
(unless there's a specialization for vector<bool>, i suppose, which
accomodates them in bits).
i'll admit that vector is generally a better choice, i'm just curious
why
you say it would take less space.
The other replies to this are only half right... Yes, vector<bool> is
a specialization of vector<T> that the standard created in order to
make it possible to pack, say, 8 bools into one byte. However, the
standard does not require this to be done, and at least accoring to
some resources (e.g. More Exceptional C++ by Sutter, a compilation of
expanded versions of his Guru of the Week) many implementations do not
make this optimization.
|
If the Standard says that vector <bool> is provided to optimize space
allocation, then I can be sure that a) a specialization exists and b) that
specialization will occupy less memory than a vector <T> for T = bool
(i.e. the non-specialized version).
Further, the Standard says that vector <bool>::reference "is a class
that simulates the behavior of references of a single bit in vector<bool>".
regards
--
jb
(replace y with x if you want to reply by e-mail)
|
|
| Back to top |
|
 |
Tim Partridge Guest
|
Posted: Fri Jul 18, 2003 2:50 pm Post subject: Re: why is a bool 4 bytes in g++? |
|
|
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:
http://www.nist.gov/dads/HTML/uppertriangm.html
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:
| Quote: | "Tim Partridge"
news:Pine.SOL.4.44.0307171203470.6965-100000 (AT) lassar (DOT) math.uwaterloo.ca...
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 .
hth
--
jb
(replace y with x if you want to reply by e-mail)
|
|
|
| Back to top |
|
 |
Tim Partridge Guest
|
Posted: Mon Jul 21, 2003 4:11 pm Post subject: Re: why is a bool 4 bytes in g++? |
|
|
On Fri, 18 Jul 2003, Tim Partridge wrote:
*snip*
| Quote: | 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.
|
|
| Back to top |
|
 |
Frank Schmitt Guest
|
Posted: Tue Jul 22, 2003 7:56 am Post subject: Re: why is a bool 4 bytes in g++? |
|
|
Tim Partridge <tjpartri (AT) lassar (DOT) math.uwaterloo.ca> writes:
| Quote: | 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?
|
Apart from the other answers, you might want to have a look at std::bitset
iff your matrix has a fixed size.
I don't know if g++ 2.95.2 already had it, though.
HTH & kind regards
frank
--
Frank Schmitt
4SC AG phone: +49 89 700763-0
e-mail: frank DOT schmitt AT 4sc DOT com
|
|
| Back to top |
|
 |
Christoph Rabel Guest
|
Posted: Wed Jul 30, 2003 8:41 am Post subject: Re: why is a bool 4 bytes in g++? |
|
|
stephan beal wrote:
| Quote: | Forgive my ignorance, but why would a vector<bool> use less space than
(e.g.) an array of bool? The bool members are the same size, either way
(unless there's a specialization for vector<bool>, i suppose, which
accomodates them in bits).
|
There is a specialization vector<bool>. And this specialization stores
bits not bools.
Christoph
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|