3

Possible Duplicate:
What does this C++ code mean?

In the following C++ code

# include <stdio.h>
int main()
{
  struct clap
  {
   int i:2;
   int j:2;
   int k:3;
   }x={1,2,3};

  printf("%d %d %d",x.i,x.j,x.k);

  return 0;
}

On running the following code, I get the output 1 -2 3

Please Explain the meaning of the ":" operator in reference to the above code, and the reason for this strange output;

Community
  • 1
  • 1
amol_beast
  • 281
  • 1
  • 2
  • 8
  • Possible duplicate: [/questions/14336994/c-structure-padding/14337041#14337062](http://stackoverflow.com/questions/14336994/c-structure-padding/14337041#14337062) – Theocharis K. Jan 15 '13 at 12:31

4 Answers4

8

These indicate bit-fields with the length being denoted after the colon

  struct clap
  {
   int i:2; // length 2
   int j:2; // length 2
   int k:3; // length 3
   };

Bitfields save on space. Try computing sizeof(clap) and you'll find it is 4 bytes on gcc 4.7. The reason it is not 1 byte (2 + 2 + 3 = 7 bits < 1 byte), is that compilers also align structs on certain boundaries depending on the underyling type of the bitfields. E.g. changing int to short or char as the underlying type of the bitfields will reduce the total size of clap to respectively 2 and 1 byte(s) (on gcc 4.7 again).

This should be compared to storing 3 full integers typically takes 12 bytes (if an int is 4 bytes). OTOH, bit-fields can make your code slower because addressing the members entails shifting and unpacking the bitfields.

The sign problem arises because the 2-bits two's complement of 2 is equal to -2. Extending the code to int j:3 will output 2.

TemplateRex
  • 69,038
  • 19
  • 164
  • 304
  • On running your code , the size of clap comes 4. Please explain how it is coming? – amol_beast Jan 15 '13 at 12:39
  • You can change the underlying type, using short instead of int, and look at the difference. – Marc Glisse Jan 15 '13 at 12:43
  • @amol_beast the size of clap is at least the size of the underlying bit-field type. E.g. having only `int i:1` as struct member, already gives `clap` size equal to 4 bytes. – TemplateRex Jan 15 '13 at 14:51
5

The colon in the structure means that the members are bit fields. That is, each field only uses the specified number of bits.

That you get -2 for the field j is probably because printf treats is as a sign-extended integer.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
2

That is what's called bit field.

Update:

The reason that x.j was printed as -2: x.j is 2, but it only has 2 bits and the higher bit is 1, which will be taken as the sign bit by printf with %d format. To verify that, just change %d to %u and see what happens.

Hui Zheng
  • 10,084
  • 2
  • 35
  • 40
1

Like other answers say it is bit field. One important note about them:

Thus C++0x treats all (non-zero-length) bit-fields in a contiguous sequence of bit-fields as part of a single memory location; an assignment to one conflicts with an assignment to any of the others. Such contiguous bit-fields should not be updated concurrently by different threads. Usually that means they should be protected by a single lock.

http://www.hpl.hp.com/personal/Hans_Boehm/c++mm/threadsintro.html#bitfields

NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277