您现在的位置: 中国男护士网 >> 考试频道 >> 计算机等级 >> 二级辅导 >> C十十 >> 辅导 >> 正文    
  C++技巧:通过pragmapack(n)改变字节对齐 【注册男护士专用博客】          

C++技巧:通过pragmapack(n)改变字节对齐

www.nanhushi.com     佚名   不详 

  通过#pragma pack(n)改变C编译器的字节对齐方式,在C语言中,结构是一种复合数据类型,其构成元素既可以是基本数据类型(如int、long、float等)的变量,也可以是一些复合数据类型(如数组、结构、联合等)的数据单元。在结构中,编译器为结构的每个成员按其自然对界(alignment)条件分配空间。看下面例子。
  其输出是:
  sizeof(char)=1 sizeof(int)=4 sizeof(short)=2 sizeof(long)=4
  struct1(char,int,short,long) offset: 0 4 8 12 sizeof(mystruct1)=16
  struct2(char,int,short,long) offset: 0 2 6 8 sizeof(mystruct2)=12
  struct3(char,int,short,long) offset: 0 1 5 7 sizeof(mystruct3)=11
  struct3(char,int,short,long) offset: 0 4 8 12 sizeof(mystruct3)=16
  #include
  /*默认字节对齐*/
  struct mystruct1{
  char a;
  int b;
  short c;
  long d;
  };
  /*2字节对齐*/
  #pragma pack(2)
  struct mystruct2{
  char a;
  int b;
  short c;
  long d;
  };
  /*1字节对齐*/
  #pragma pack(1)
  struct mystruct3{
  char a;
  int b;
  short c;
  long d;
  };
  /*恢复默认字节对齐*/
  #pragma pack()
  struct mystruct4{
  char a;
  int b;
  short c;
  long d;
  };
  int main(int argc,char* argv[]){
  int a_off;
  int b_off;
  int c_off;
  int d_off;
  struct mystruct1 s1;
  struct mystruct2 s2;
  struct mystruct3 s3;
  struct mystruct4 s4;
  printf("sizeof(char)=%d sizeof(int)=%d sizeof(short)=%d sizeof(long)=%d\n",
  sizeof(char), sizeof(int), sizeof(short), sizeof(long));
  /*mystruct1*/
  a_off = (char*)&(s1.a) - (char*)&s1;
  b_off = (char*)&(s1.b) - (char*)&s1;
  c_off = (char*)&(s1.c) - (char*)&s1;
  d_off = (char*)&(s1.d) - (char*)&s1;
  printf("struct1(char,int,short,long) offset: %d %d %d %d",a_off,b_off,c_off,d_off);
  printf(" sizeof(mystruct1)=%d\n",sizeof(mystruct1));
  /*mystruct2*/
  a_off = (char*)&(s2.a) - (char*)&s2;
  b_off = (char*)&(s2.b) - (char*)&s2;
  c_off = (char*)&(s2.c) - (char*)&s2;
  d_off = (char*)&(s2.d) - (char*)&s2;
  printf("struct2(char,int,short,long) offset: %d %d %d %d",a_off,b_off,c_off,d_off);
  printf(" sizeof(mystruct2)=%d\n",sizeof(mystruct2));
  /*mystruct3*/
  a_off = (char*)&(s3.a) - (char*)&s3;
  b_off = (char*)&(s3.b) - (char*)&s3;
  c_off = (char*)&(s3.c) - (char*)&s3;
  d_off = (char*)&(s3.d) - (char*)&s3;
  printf("struct3(char,int,short,long) offset: %d %d %d %d",a_off,b_off,c_off,d_off);
  printf(" sizeof(mystruct3)=%d\n",sizeof(mystruct3));
  /*mystruct4*/
  a_off = (char*)&(s4.a) - (char*)&s4;
  b_off = (char*)&(s4.b) - (char*)&s4;
  c_off = (char*)&(s4.c) - (char*)&s4;
  d_off = (char*)&(s4.d) - (char*)&s4;
  printf("struct3(char,int,short,long) offset: %d %d %d %d",a_off,b_off,c_off,d_off);
  printf(" sizeof(mystruct3)=%d\n",sizeof(mystruct4));
  return 0;
  }

 

文章录入:杜斌    责任编辑:杜斌 
  • 上一篇文章:

  • 下一篇文章:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
     

    联 系 信 息
    QQ:88236621
    电话:15853773350
    E-Mail:malenurse@163.com
    免费发布招聘信息
    做中国最专业男护士门户网站
    最 新 热 门
    最 新 推 荐
    相 关 文 章
    没有相关文章
    专 题 栏 目

      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)                            【进男护士社区逛逛】
    姓 名:
    * 游客填写  ·注册用户 ·忘记密码
    主 页:

    评 分:
    1分 2分 3分 4分 5分
    评论内容:
  • 请遵守《互联网电子公告服务管理规定》及中华人民共和国其他各项有关法律法规。
  • 严禁发表危害国家安全、损害国家利益、破坏民族团结、破坏国家宗教政策、破坏社会稳定、侮辱、诽谤、教唆、淫秽等内容的评论 。
  • 用户需对自己在使用本站服务过程中的行为承担法律责任(直接或间接导致的)。
  • 本站管理员有权保留或删除评论内容。
  • 评论内容只代表网友个人观点,与本网站立场无关。