Why does GCC use this bit field?

The program is in C using std = c99, it is on a 64-bit machine.

struct epochs {
    volatile unsigned int epoch    : 1;
    volatile unsigned int pulse    : 1;
    volatile unsigned int active0  : 7;
    volatile unsigned int active1  : 7;
    volatile unsigned int counter0 : 24; 
    volatile unsigned int counter1 : 24; 
};

when i check sizeof (epochs) it gives me 12.

I can tell gcc not to add it by adding __attribute ((packed)); so I can get around this. However, I would really like to know why 4 bytes were added to fill this 64-bit structure?

The main thing is that this structure MUST be 64 bits, because it is updated immediately in 64-bit atomic swap operations, which, of course, will not work with a 12-byte value.

+4
source share
2 answers
volatile unsigned int epoch    : 1;
volatile unsigned int pulse    : 1;
volatile unsigned int active0  : 7;
volatile unsigned int active1  : 7;

^ 32-bit (4 bytes)

volatile unsigned int counter0 : 24; 

^ 32-bit (4 bytes)

volatile unsigned int counter1 : 24; 

^ 32-bit (4 bytes)

So, another 4 bytes.

C says:

(C99, 6.7.2.1p10) " , -, , "

24- (counter0) 32- (, unsigned int ), 16- (epoch, pulse, active0, active1).

uin64_t unsigned int, - 64- , , .

(C99, 6.7.2.1p4) " , _Bool, int, unsigned int, - , ."

+12

int foo:3; , . long foo:3 short foo:3, foo , C , . , , , , . , 24- - , , 32- , 8 , 24- ( ), "" 32- .

, , . , unsigned long long, [ 32- , " t 64- ].

+1

All Articles