Chinaunix首页 | 论坛 | 博客
  • 博客访问: 290934
  • 博文数量: 61
  • 博客积分: 1581
  • 博客等级: 上尉
  • 技术积分: 741
  • 用 户 组: 普通用户
  • 注册时间: 2012-10-24 13:52
个人简介

幸运儿不是我,因为我选择的路很难走.如果够出色也不能出头,至少要做到没第二个我.

文章分类

全部博文(61)

文章存档

2013年(14)

2012年(47)

分类: LINUX

2012-10-25 18:51:15

简单地说,struct的sizeof是所有成员对齐后长度相加,而union的sizeof是取最大的成员长度

 

     C++ 编译器为了使CPU的性能达到最佳, 会对 struct 的内存结构进行优化,如32位的计算机的数据传输值是4 bytes, 64位计算机 数据传输是 8 bytes,这样,struct 在默认的情况上,编译器会对 struct 的结构进行数据对齐((32位机)4的倍数或(64 位机)8的倍 数), 以下以32位机在vc6.0环境下:

 

例1:struct s1

{    

  char ch;                           //1bit+对齐

  char *ptr;                          //4bytes

  union

 {

   short a, b;

   unsigned int c:2, d:1;

 }un ;                              //4 bytes

 struct s1 *next;                  //4 bytes 

}st;

 

struct data 

   int i;                     //4

   char *ch;                 //4

   double f;                 //8 

}b;

 printf("%d\n",sizeof(b));          //结果:16

 printf("%d\n",sizeof(st.un));       //结果:4

 printf("%d\n",sizeof(st));         //结果:16

 

例2:

struct s1 

{

   short d;      //2bytes+2bytes对齐

   int a;

   short b;     //2bytes+2bytes对齐

}a1;

 

struct s2

 short d;      //2bytes

 short b;      //2bytes

 int a;       //4bytes 

}a2;

 

struct s3

{

  short d;     //2bytes+2bytes对齐

  int a;

  short b;     //2bytes

  char st;     //1bytes+1bites对齐

}a3;

  printf("c=%d\n " ,sizeof(a1));      //结果:12

  printf("c=%d\n " ,sizeof(a2));      //结果:8

  printf("c=%d\n " ,sizeof(a3));      //结果:12

 

自然对界

    struct 是一种复合数据类型,其构成元素既可以是基本数据类型(如int、long、float 等)的变量,也可以是一些复合数据类型(如array、struct、union 等)的数据单元。对于结构体,编译器会自动进行成员变量的对齐,以提高运算效率。缺省情况下,编译器为结构体的每个成员按其自然对界(natural alignment)条件分配空间。各个成员按照它们被声明的顺序在内存中顺序存储,第一个成员的地址和整个结构的地址相同。

自然对界(natural alignment)即默认对齐方式,是指按结构体的成员中size 最大的成员对齐。

例如:

struct naturalalign

{

char a;

short b;

char c;

};

在上述结构体中,size 最大的是short,其长度为2 字节,因而结构体中的char 成员a、c 都以2 为单位对齐,sizeof(naturalalign)的结果等于6;

如果改为:

struct naturalalign

{

char a;

int b;

char c;

};

其结果显然为12。

 

指定对界

一般地,可以通过下面的方法来改变缺省的对界条件:

· 使用伪指令#pragma pack (n),编译器将按照n 个字节对齐;

· 使用伪指令#pragma pack (),取消自定义字节对齐方式。

注意:如果#pragma pack (n)中指定的n 大于结构体中最大成员的size,则其不起作用,结构体仍然按照size 最大的成员进行对界。

 

例如:

 

#pragma pack (n)

struct naturalalign

{

char a;

int b;

char c;

};

#pragma pack ()

当n 为4、8、16 时,其对齐方式均一样,sizeof(naturalalign)的结果都等于12。

 

而当n 为2时,其发挥了作用,使得sizeof(naturalalign)的结果为6。

 

例3(面试题):#include

 

#pragma pack(8)

 

struct example1

 

{

 

     short a;

 

     long b;

 

};

 

struct example2

 

{

     char c;

     example1 struct1;

     short e;

};

#pragma pack()

int main(int argc, char* argv[])

{

     example2 struct2;

     cout << sizeof(example1) << endl;                //结果:8

     cout << sizeof(example2) << endl;                //结果:16

 cout << (unsigned int)(&struct2.struct1) - (unsigned int)(&struct2) << endl; //结果:4

     return 0;

}

解析:

    程序中#pragma pack (8)虽然指定了对界为8,但是由于struct example1 中的成员最大size 为4(long 变量size 为4),故struct example1 仍然按4 字节对界,struct example1 的size为8,即第18 行的输出结果;

 

    struct example2 中包含了struct example1,其本身包含的简单数据成员的最大size 为2(short变量e),但是因为其包含了struct example1,而struct example1 中的最大成员size 为4,structexample2 也应以4 对界,#pragma pack (8)中指定的对界对struct example2 也不起作用,故sizeof(example2)的输出结果为16;

   由于struct example2 中的成员以4 为单位对界,故其char 变量c 后应补充3 个空,其后才是成员struct1 的内存空间,20 行的输出结果为4

阅读(1318) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~