-1

I think it should be 1. Because 1 byte is enough to hold this BOOL. And if I comment "BOOL b:1", it becomes a empty struct, and its size is 1 now.

#include <iostream>
#include <string>
using namespace std;

enum BOOL { FALSE=0, TRUE=1 };
struct A {
  BOOL b:1;
};

int main()
{
  std::cout << sizeof(A)  << std::endl; //output 4.
}
Jack Chin
  • 525
  • 4
  • 12

1 Answers1

2

Padding.

If you want the struct to be "packed" (good for memory use, TERRIBLE for performance), you can ask for that in a compiler-specific nonstandard way.

Borealid
  • 95,191
  • 9
  • 106
  • 122
  • I know there is padding. But one byte is enough to do the padding I think. And do you mean that a empty struct will have terrible performance. I don't think so. – Jack Chin Sep 15 '15 at 02:22
  • Read up on "access alignment". Most CPUs cannot access an arbitrary two bytes in a performant fashion. – Borealid Sep 15 '15 at 05:32