tags:

views:

103

answers:

4

If 1 bool is 1byte [8 bits] then would a packed structure of 4 bools be 32bits or 4? The pack directive removes the alignment requirement, but would it make sets of bools more efficient [memory wise]?

+2  A: 

Yes. Even a packed structure of booleans will use at least 8 bits per boolean. Unless you use bit fields.

John Calsbeek
A: 

That is an implementation-defined. The standard doesn't define the size of a boolean.

EDIT

This is from the standard :

5.3.3 Sizeof

The sizeof operator yields the number of bytes in the object representation of its operand. The operand is either an expression, which is not evaluated, or a parenthesized type-id. The sizeof operator shall not be applied to an expression that has function or incomplete type, or to an enumeration type before all its enumerators have been declared, or to the parenthesized name of such types, or to an lvalue that designates a bit-field. sizeof(char), sizeof(signed char) and sizeof(unsigned char) are 1; the result of sizeof applied to any other fundamental type (3.9.1) is implementation-defined. [Note: in par- ticular, sizeof(bool) and sizeof(wchar_t) are implementation-defined.69) ] [Note: See 1.7 for the definition of byte and 3.9 for the definition of object representation. ]

VJo
It does specify that a `bool` object must be at least one byte, though (as MSN explains in his answer, you have to be able to address individual objects of type `bool`).
James McNellis
@James McNellis As you can see, the standard doesn't specify the size of bool. As far as it is concerned, it can be 20 bytes.
VJo
@VJo: You may be answering a different question. The OP has asked, assuming `sizeof(bool) == 1`, can the size of some packed structure containing four `bool` elements be less than `sizeof(bool) * 4`. The answer to that is "no." A object cannot be smaller than one byte (unless it's a bitfield, but then the things you can do with the object are limited). The question isn't so much about `sizeof(bool)` as it is about structure packing.
James McNellis
You are right. I answered wrong question :(
VJo
A: 

4 bools.

Each bool needs a unique address (as you can take a bool's address). If you use a bitfield, you can reduce the size to 1 bool, but you won't be able to get the address of an individual bitfield.

MSN
+1  A: 

The size of a bool could possibly vary from OS to OS and language to language. I've seen it being a byte, a word and an int (which in turn could be anything as well). But if sizeof(bool) is 1, then a packed structure of bools will be 4 (bytes) (thus 32 bits)

Rather than messing with packing and alignment, why not use:

std::vector<bool>

From : http://www.cplusplus.com/reference/stl/vector/

It is optimized (or should be) internally to be a bitfield. Try it, you'll see the memory it uses is consistent with a single bit per value.

Otherwise you can always roll your own library or use the limited FD_SET macros.

Marius
`vector<bool>` is a mess; if you need something like it, use `bitset` instead. http://www.gotw.ca/gotw/050.htm
Steve M
One would think so, as I initially did, then one realizes that bitset is fixed in size at compile time (which is not always good) as well as for some reason (speculated about stack size) I am unable to do allocate a mere 120Mb: `bitset<1000000000> test` (I get a segfault on OS X, and it failed on win32) yet I am able to do `vector<bool> test(1000000000)` no problem (albeit slowly!). So vector is dynamic AND handles larger values. Go figure.
Marius
Good point. And that's when I turn to boost's dynamic bitset.
Steve M