1

I have below program.

#include<stdio.h>                                                                                                                                                                                    
#include<string.h>                                                                                                                                                                                   

struct abc {                                                                                                                                                                                         
  int i;                                                                                                                                                                                             
  char ar[10];                                                                                                                                                                                       
  int j:9;                                                                                                                                                                                           
  int k:3;                                                                                                                                                                                           
}abc1;                                                                                                                                                                                               


void wasim_dump_data(const void *object, size_t size)
{                                                    
  int i;                                             
  printf("[ \n");                                    
  for(i = 0; i < size; i++)                          
  {                                                  
    if (i%4 ==0)                                     
      printf("[ ");                                  
    printf("%02x ", ((const unsigned char *) object)[i] & 0xff);
    if ((i+1)%4 == 0)                                           
      printf("]\n");                                            
  }                                                             
  printf("]\n");                                                

}

int main ()
{          
  strcpy (abc1.ar, "wasim");
  abc1.i=5;                 
  abc1.j=2;                 
  abc1.k=3;                 

  struct abc * p1 = &abc1;
  printf("abc1.ar : %s\nabc1.i : %d\nabc1.j : %d\nabc1.k : %d\n",abc1.ar, abc1.i, abc1.j, abc1.k);
  wasim_dump_data (p1,sizeof(abc1));                                                              

  return 0;
}

which gives the below output

abc1.ar : wasim
abc1.i : 5
abc1.j : 2
abc1.k : 3
[
[ 05 00 00 00 ]
[ 77 61 73 69 ]
[ 6d 00 00 00 ]
[ 00 00 02 06 ]
]

How come 2 and 3 stored in memory as 02 and 06. How odd sized bitfields which size is more than a byte stored in memory?

Jens
  • 69,818
  • 15
  • 125
  • 179
Sriram.K
  • 45
  • 7

1 Answers1

2

The compiler packs the bitfields as follows in the last two bytes:

  JJJJJJJJ00000KKKJ
  00000010000000110 = 02 06

where the final J is the most significant bit of j. Note that almost everything about bitfields is implementation-defined or unspecified so you cannot rely on this. If you need total control of the layout, don't use bitfields but unsigned types and bit shifting plus masking.

Jens
  • 69,818
  • 15
  • 125
  • 179
  • Thank you very much. Can you please tell why the integer packed to 16 bits here? – Sriram.K May 12 '14 at 08:46
  • That's unspecified. The compiler writer is free to chose. Apparently she chose to pack for minimum size. Since 3 + 9 <= 16, two bytes are enough. With other compiler options the result may even be different. – Jens May 12 '14 at 09:04