C ++ Bitfield Packing mit Bools

Ich habe gerade einen Test mit Bitfeldern durchgeführt und die Ergebnisse überraschen mich.

class test1 {
public:
    bool test_a:1;
    bool test_b:1;
    bool test_c:1;
    bool test_d:1;
    bool test_e:1;
    bool test_f:1;
    bool test_g:1;
    bool test_h:1;
};

class test2 {
public:
    int test_a:1;
    int test_b:1;
    int test_c:1;
    int test_d:1;
    int test_e:1;
    int test_f:1;
    int test_g:1;
    int test_h:1;
};

class test3 {
public:
    int test_a:1;
    bool test_b:1;
    int test_c:1;
    bool test_d:1;
    int test_e:1;
    bool test_f:1;
    int test_g:1;
    bool test_h:1;
};

Die Ergebnisse waren: -

sizeof(test1) = 1   // This is what I'd expect. 8 bits in a byte
sizeof(test2) = 4   // Reasonable. Maybe padded out to the size of an int.
sizeof(test3) = 16  // What???

Ist das, was Sie erwarten würden, oder ein Compiler-Fehler? (Codegear C ++ Builder 2007, übrigens ...)

Antworten auf die Frage(12)

Ihre Antwort auf die Frage