Acabo de hacer una prueba con bitfields, y los resultados me sorprenden.C++ bitfield packing con bools
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;
};
Los resultados fueron los siguientes: -
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???
Es esto lo que cabría esperar, o un error del compilador? (Codegear C++ Builder 2007, por cierto ...)
Si desea tener más control sobre el diseño de las estructuras de campo de bits en la memoria, considere usar este campo de bits, implementado como un archivo de encabezado de biblioteca: [link] (https://github.com/wkaras/C- plus-plus-library-bit-fields/blob/master/Bitfield.pdf) – WaltK