免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 630 | 回复: 0
打印 上一主题 下一主题

使用__attribute__处理对齐问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-11-24 19:49 |只看该作者 |倒序浏览

                GNU C扩展的__attribute__ 机制被用来设置函数、变量、类型的属性,其用得较多的是处理字节对齐的问题。
__attribute__ 的语法为:
      
__attribute__ ((语法列表))
参数aligned(number) [number为最小对齐的字节数]是用得较多的一个。
另一个是参数packed 表示“使用最小对齐”方式,即对变量是字节对齐,对于域是位对齐。
这个例子稍长了点,不过非常简单:
[root@Kendo develop]# cat align.c
  1. #include
  2. struct A{
  3.          char a;
  4.          int b;
  5.          unsigned short c;
  6.          long d;
  7.          unsigned long long e;
  8.          char f;
  9. };
  10. struct B{
  11.          char a;
  12.          int b;
  13.          unsigned short c;
  14.          long d;
  15.          unsigned long long e;
  16.          char f;
  17. }__attribute__((aligned));
  18. struct C{
  19.          char a;
  20.          int b;
  21.          unsigned short c;
  22.          long d;
  23.          unsigned long long e;
  24.          char f;
  25. }__attribute__((aligned(1)));
  26. struct D{
  27.          char a;
  28.          int b;
  29.          unsigned short c;
  30.          long d;
  31.          unsigned long long e;
  32.          char f;
  33. }__attribute__((aligned(4)));
  34. struct E{
  35.          char a;
  36.          int b;
  37.          unsigned short c;
  38.          long d;
  39.          unsigned long long e;
  40.          char f;
  41. }__attribute__((aligned(8)));
  42. struct F{
  43.          char a;
  44.          int b;
  45.          unsigned short c;
  46.          long d;
  47.          unsigned long long e;
  48.          char f;
  49. }__attribute__((packed));
  50. int main(int argc, char **argv)
  51. {
  52.          printf("A = %d, B = %d, C = %d, D = %d, E = %d, F = %d\n",
  53.                 sizeof(struct A), sizeof(struct B), sizeof(struct C), sizeof(struct D), sizeof(struct E), sizeof(struct F));
  54.          return 0;
  55. }
复制代码
在一个32位机上运行结果如下:
  1. [root@Kendo develop]# gcc -o align align.c
  2. [root@Kendo develop]# ./align            
  3. A = 28, B = 32, C = 28, D = 28, E = 32, F = 20
复制代码
我们看到,最后一个struct F,1 + 4 + 2 + 4 + 8 + 1 = 20,因为使用了__attribute__((packed)); 来表示以最小方式对齐,所以结果刚好为20。
而第一个struct A,因为什么也没有跟,采用默认处理方式:4(1) + 4 + 4(2) + 4 +    8 + 4(1) = 28,括号中是其成员本来的大小。与此相似的是struct D。
接下来看struct E,采用8个字节的方式来对齐:8(1+4+2 ,即a, b, c)+ 8(4, d) + 8 + 8(1, f) = 32。
而在struct C中,试图使用__attribute__((aligned(1))) 来使用1个字节方式的对齐,不过并未如愿,仍然采用了默认4个字节的对齐方式。
在struct B中,aligned没有参数,表示“让编译器根据目标机制采用最大最有益的方式对齐"——当然,最有益应该是运行效率最高吧,呵呵。其结果是与struct E相同。
在对结构的大小并不关注的时候,采用默认对齐方式或者编译器认为最有益的方式是最常见的,然后,对于一些对结构空间大小要求严格,例如定义一个数据包报头的时候,明白结构的对齐方式,就非常有用了。
               
               
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/67414/showart_1663707.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP