46

Possible Duplicate:
What does ‘unsigned temp:3’ means

I'm learning some kernel code, and came along the following line (in linux 2.4, sched.h, struct mm_struct):

unsigned dumpable:1;

What does this mean?

dbc
  • 104,963
  • 20
  • 228
  • 340
Tzafrir
  • 1,801
  • 2
  • 16
  • 18
  • trinary is base three. You meant ternary, and though it's the only ternary operator, as John states that's not its name. – Pete Kirkham Jul 06 '10 at 12:13
  • Thanks, I appreciate these corrections. I removed that line altogether from my question as it was not relevant. – Tzafrir Jul 06 '10 at 12:21
  • 4
    I believe that the colon tag should remain on this question, as others might, like me, use that word to try and find out what this means before asking. – Tzafrir Jul 06 '10 at 12:23

3 Answers3

59

It's a bitfield member. Your code means dumpable occupies exactly 1 bit in the structure.

Bitfields are used when you want to pack members in bit-level. This can greatly reduce the size of memory used when there are a lot of flags in the structure. For example, if we define a struct having 4 members with known numeric constraint

0 < a < 20
    b in [0, 1]
0 < c < 8
0 < d < 100

then the struct could be declared as

struct Foo {
   unsigned a : 5;   // 20 < 2^5 = 32
   unsigned b : 1;   // 
   unsigned c : 3;   // 
   unsigned d : 7;   // 100 < 2^7 = 128
};

then the bits of Foo may be arranged like

                      ddddddd c  cc b aaaaa
---------  ---------  ---------  ----------
                       octet 1     octet 0
===========================================
                uint32

instead of

struct Foo {
   unsigned a;
   unsigned b;
   unsigned c;
   unsigned d;
};

in which many bits are wasted because of the range of values

# wasted space which is not used by the program
# v                                     v
                             ddddddd                                  ccc
------------------------------------ ------------------------------------
            uint32                                 uint32


                                   b                                aaaaa
------------------------------------ ------------------------------------
            uint32                                 uint32

so you can save space by packing many members together.

Note that the C standard doesn't specify how the bitfields are arranged or packed within an "addressable storage unit". Also, bitfields are slower compared with direct member access.

kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
  • 5
    Thanks for a great answer! It's worth explicitly noting, for the sake of the casual reader, that the implementation of these bit fields is not only machine dependent, but also compiler dependent, and code that uses assumptions on the location of these bits in memory is highly unportable. – Tzafrir Jul 06 '10 at 12:49
  • 1
    the C99 Standard defines that bitfields are placed adjacent to each other when enough space is avaliable. "An implementation may allocate any addressable storage unit large enough to hold a bitfield. 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." – waynix Apr 08 '13 at 12:27
5

It means it's a bitfield - i.e. the size of dumpable is a single bit, and you can only assign 0 or 1 to it. Normally used in old code to save space, or in low-level code that interfaces with hardware (even though the packing is non-portable). See here for more information

Airsource Ltd
  • 32,379
  • 13
  • 71
  • 75
  • The packing is not necessarily as it seems as you point out correctly. Often the bitfields are aligned at 4 byte boundaries. There are however compiler options which influence this. – GorillaPatch Jul 06 '10 at 12:24
3

If I remember correctly, when used inside of a struct the number after the colon signifies how many bits make up the variable (or a bitfield).

So unsigned dumpable:1; is a single bit bitfield.

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536