gcc中的结构体成员位域

1===============================================

struct test {

  int m: 1;  //此字段使用一位

};

sizeof(struct test)  使用x86_64的gcc本地编译运行得到结果为4

 2===============================================

 struct test {

  int a0: 1;

  int a1: 1;

  int a2: 1;

  .....

  int a31: 1;

};

sizeof(struct test) 使用x86_64的gcc本地编译运行得到结果为4

3 ===============================================

 

struct test {

  int a0: 1;

  int a1: 1;

  .....

  int a31: 1;

  int b0: 1;

};

sizeof(struct test) 使用x86_64的gcc本地编译运行得到结果为8

 

得出结论,编译器以较大成员对齐结构体的(描述不太准确)。 成员后面的冒号表示指定该成员位数,后面的数字为位数值,必须为非负整数,不然编译报错。有此指定后表示此成员为位域成员,不能使用sizeof运算符求值,不然编译报错。结构中的普通成员可以使用sizeof运算符求值

 

posted @ 2015-01-31 22:16  初次见面两人齐齐心动  阅读(565)  评论(0编辑  收藏  举报