14
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?

Chubsdad
  • 24,777
  • 4
  • 73
  • 129
Johnny
  • 1,963
  • 4
  • 21
  • 24

7 Answers7

18

No, the sizeof your buffer is implementation defined, as per this quote from the Standard:

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

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

Mike 'Pomax' Kamermans
  • 49,297
  • 16
  • 112
  • 153
Chubsdad
  • 24,777
  • 4
  • 73
  • 129
  • 6
    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). – zwol Oct 30 '10 at 00:54
7

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

Anycorn
  • 50,217
  • 42
  • 167
  • 261
4

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;
}
  • 4
    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. –  Oct 29 '10 at 05:35
4

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

binW
  • 13,220
  • 11
  • 56
  • 69
2

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

Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
2

Use a bitset if you want to be sure each bit will be considered as a bit instead of using a byte for a whole value:

#include <bitset>
using namespace std;
#define SIZE 1000;
int main()
{
bitset<SIZE> bit_set; // unfortunately the size of a bitset is determined at compile time
bit_set.flip();
bit_set[232] = true;
}

You have to understand this is a very low level memory constraint of processors and their instructions, as they are designed to support words of bits, not bits. It would have been a good thing that some instructions would have been added for this purpose though, since the thing bitset does is just bit shifting...

I really need to learn x86 assembly.

jokoon
  • 6,207
  • 11
  • 48
  • 85
0

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

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358