A bool
takes 1 byte in C++. But, why does a bool[8]
take 8 bytes instead of 1 byte?
One byte has enough space for 8 bits.
I compiled this with GCC using -Os
flag:
#include <iostream>
using namespace std;
class Foo
{
public:
bool m_bool[8];
};
int main ()
{
cout << "Size: " << sizeof(Foo) << " byte(s) " << endl;
return 0;
}
It returns "Size: 8 byte(s)".
Is there a way to optimize it?