Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ bitfield packing with bools

Tags:

c++

bit-fields

I've just done a test with bitfields, and the results are surprising me.

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; }; 

The results were:-

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

Is this what you'd expect, or a compiler bug? (Codegear C++ Builder 2007, btw...)

like image 804
Roddy Avatar asked Nov 21 '08 10:11

Roddy


People also ask

What is Bitfield in C with example?

Bit Fields in C Language In programming terminology, a bit field is a data structure that allows the programmer to allocate memory to structures and unions in bits in order to utilize computer memory in an efficient manner.

How many bytes are bools?

Boolean variables are stored as 16-bit (2-byte) numbers, but they can only be True or False.

How are bit fields stored in memory C?

Again, storage of bit fields in memory is done with a byte-by-byte, rather than bit-by-bit, transfer.

What is the use of bit field in C?

In C, we can specify size (in bits) of structure and union members. The idea is to use memory efficiently when we know that the value of a field or group of fields will never exceed a limit or is within a small range.


2 Answers

your compiler has arranged all of the members of test3 on integer size boundaries. Once a block has been used for a given type (integer bit-field, or boolean bit-field), the compiler does not allocate any further bit fields of a different type until the next boundary.

I doubt it is a bug. It probably has something to do with the underlying architecture of your system.

edit:

c++ compilers will allocate bit-fields in memory as follows: several consecutive bit-field members of the same type will be allocated sequentially. As soon as a new type needs to be allocated, it will be aligned with the beginning of the next logical memory block. The next logical block will depend on your processor. Some processors can align to 8-bit boundaries, while others can only align to 16-bit boundaries.

In your test3, each member is of a different type than the one before it, so the memory allocation will be 8 * (the minimum logical block size on your system). In your case, the minimum block size is two bytes (16-bit), so the size of test3 is 8*2 = 16.

On a system that can allocate 8-bit blocks, I would expect the size to be 8.

like image 125
e.James Avatar answered Sep 17 '22 20:09

e.James


Be careful with bitfields as much of its behavior is implementation (compiler) defined:

From C++03, 9.6 Bitfields (pg. 163):

Allocation of bit-fields within a class object is implementation-defined. Alignment of bit-fields is implementation-defined. Bit-fields are packed into some addressable allocation unit. [Note:bit-fields straddle allocation units on some machines and not on others. Bit-fields are assigned right-to-left on some machines, left-to-right on others. ]

That is, it is not a bug in the compiler but rather lack of a standard definition of how it should behave.

like image 20
David Rodríguez - dribeas Avatar answered Sep 19 '22 20:09

David Rodríguez - dribeas