3

Here I have a code snippet.

#include <stdio.h>
int main()
{
    struct value
    {
        int bit1 : 1;
        int bit2 : 4;
        int bit3 : 4;
    } bit;

    printf("%d",sizeof(bit));

    return 0;
}

I'm getting the output as 4 (32 bit compiler). Can anyone explain me how? Why is it not 1+ 4 + 4 = 9? I've never worked with bit fields before so would love some help. Thank you. :)

NirAv JaIn
  • 1,035
  • 1
  • 9
  • 16

3 Answers3

7

When you tell the C compiler this:

int bit1 : 1

It interprets it as, and allocates to it, an integer; but refers to it's first bit as bit1.

So if we consider your code:

struct value
{
    int bit1 : 1;
    int bit2 : 4;
    int bit3 : 4;
} bit;

What you are telling the compiler is this: Take necessary number of the ints, and refer to the chunks bit 1 as bit1, then refer to bits 2 - 5 as bit2, and then refer to bits 6 - 9 as bit3.

Since the complete number of bits required are 9, and an int is 32 bits (in your computer's architecture), memory space of only 1 int is required. Thus you get the size as 4 (bytes).

Instead, if you were to define the struct using chars, since char is 8 bits, the compiler would allocate the memory space of two chars for each struct value. And you will get 2 (bytes) as your output.

Re Captcha
  • 3,125
  • 2
  • 22
  • 34
sampathsris
  • 21,564
  • 12
  • 71
  • 98
5

Because C requests to pack the bits in the same unit (here one signed int / unsigned int):

(C99, 6.7.2.1p10) "If enough space remains, a bit-field that immediately follows another bit-field in a structure shall be packed into adjacent bits of the same unit"

ouah
  • 142,963
  • 15
  • 272
  • 331
0

The processor just likes chucking around 32 bits in one go - not 9, 34 etc.

It just rounds it up to what the processor likes. (Keep the worker happy)

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
  • I think there is a bit more than that. I agree with you when you say `not 9, 34`, obvisouly the size will be a power of 2 bytes (1, 2, 4, etc.). But with the above example, using `char` instead of `int` change the size to `2` from `4` so maybe there is something depending on the type of bit fields? My understanding is that with bitfields declared as `int`, the size of the structure cannot be less than the one of a `int` (i.e. 4 bytes), but with `char` it can shrink til 1 byte. – Holt Jul 13 '14 at 17:18
  • @holt - It is the size of the bus etc. It likes and can only deal with 32 bits at a time.. So you get the lot. – Ed Heal Jul 13 '14 at 17:21
  • 1
    If everything was aligned according to the bus size, then everything would be aligned to 4 bytes on a 32-bits architecture, which is not the case (you can aligne on 1, 2, 4, 8, etc. bytes), as my example shows (if you change `int` to `char` in the OP example, the `sizeof` returns 2, not 4). – Holt Jul 13 '14 at 17:24
  • What is the `sizeof` operator to do with alignment? It says that X can be fitted into Y bits. Does not mention that you have Z bits in the trimmings – Ed Heal Jul 13 '14 at 17:41
  • The `sizeof` has nothing to do with alignment, but the way your answer is currently written suggest that since you're mixing the `32` bits output from the OP with `what processor likes`. I'm just pointing the fact that the `32` bits comes from the `int` declaration and not "what processor like". If it really was "what processor like", then the same structure with `char` declaration would take 4 bytes, which is not the case (only 2 bytes). – Holt Jul 14 '14 at 00:14
  • The size of a struct with bit fields is a compile time decision, made by the compiler. Your answer is wrong because it implies that it is a run time decision made by the processor. – sampathsris Jul 15 '14 at 11:18