views:

112

answers:

6
bool fp[81];

From my understanding fp should use ceil(81/8) bytes because it is in succession.

Am I correct?

How can I prove this?

A: 

No, a bool is 8 bits. Use vector<bool> (a specialized bit-packed vector) or bitset.

Ignacio Vazquez-Abrams
Or a boost::dynamic_bitset, as vector<bool> is evil.
Roger Pate
Why is it evil?
Johnny
@Johnny: It doesn't [meet](http://stackoverflow.com/q/670308/54262) the [container requirements](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2160.html).
Roger Pate
Bool being 8 bits is not guaranteed.
Chubsdad
-1 No, the size of `bool` is implementation defined, not necessarily 8 bits.
Alf P. Steinbach
But the size of `bool` is guaranteed to be at least `CHAR_BIT` bits. C(++) doesn't do bit-packing except for bitfields.
Zack
@Zack: And the legacy `vector<bool>`.
Ignacio Vazquez-Abrams
That's the library, not the language.
Zack
+1  A: 

No, each bool is usually stored separately (usually, depending on your computer, 8-bits). The memory occupied would be a minimum of 81 bytes.

muntoo
No. That' not guaranteed
Chubsdad
+2  A: 

no, its 81*sizeof(bool) which is most likely 81 bytes

aaa
+3  A: 

You can find out the storage used by any object or type with sizeof:

int main() {
  bool fp[81];
  cout << sizeof fp << '\n';
  cout << sizeof(bool[81]) << '\n';
  return 0;
}
Roger Pate
Note this only reports the size of the actual object without the size of any resources it controls, such as dynamically allocated memory: sizeof(std::string) is a constant, even when applied to strings of different length.
Roger Pate
A: 

you can check its size using sizeof(fp) which in my case gives 81

binW
+5  A: 

No, the sizeof your buffer is implementation defined. Refer the quote from the Standard below.

Therefore the size you can expect is 81 * X where X is the size of bool, which is implementation defined.

$5.3.3/1 - "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 particular, 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. ]

Chubsdad
This is correct, but misses one critical point, which is that `sizeof(bool)` **must be** greater than or equal to `sizeof(char)`, because `sizeof(char)` is 1 **by definition**. [Yes, this means it would be fiendishly difficult to make a conforming C implementation on a bit-addressable machine.] Therefore, whatever the size of the buffer is, it must be *at least* 81; it cannot be ceil(81/8).
Zack