Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

1 bit per bool in Array C++

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?

like image 359
Johnny Avatar asked Oct 29 '10 05:10

Johnny


1 Answers

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

like image 74
Chubsdad Avatar answered Oct 05 '22 23:10

Chubsdad