Chinaunix首页 | 论坛 | 博客
  • 博客访问: 81666
  • 博文数量: 19
  • 博客积分: 546
  • 博客等级: 中士
  • 技术积分: 232
  • 用 户 组: 普通用户
  • 注册时间: 2010-02-10 00:07
文章分类

全部博文(19)

文章存档

2014年(1)

2013年(3)

2012年(2)

2011年(4)

2010年(9)

我的朋友

分类: C/C++

2011-02-11 17:33:44

程序代码

312struct usb_interface_descriptor {
313        __u8  bLength;
314        __u8  bDescriptorType;
315
316        __u8  bInterfaceNumber;
317        __u8  bAlternateSetting;
318        __u8  bNumEndpoints;
319        __u8  bInterfaceClass;
320        __u8  bInterfaceSubClass;
321        __u8  bInterfaceProtocol;
322        __u8  iInterface;
323} __attribute__ ((packed));


Linux kernel v2.6.36 structure usb_interface_descriptor中的__attribute__ ((packed))
__attribute__ ((packed)) 的作用就是告诉编译器取消结构在编译过程中的优化对齐,按照实际占用字节数进行对齐,是GCC特有的语法。
example:
在GCC下:
struct my{
char ch;
int a;
}__attrubte__ ((packed))
sizeof(int)=4;
sizeof(my)=5

__attribute__机制是GNU C的一大特色。

__attribute__可以设置函数属性(Function Attribute)、变量属性(Variable Attribute)和类型属性(Type Attribute)。

__attribute__书写特征是:__attribute__前后都有两个下划线,并且后面会紧跟一对括弧,括弧里面是相应的 __attribute__参数。

__attribute__语法格式为:__attribute__ ((attribute-list))

其位置约束:放于声明的尾部“;”之前。

packed属性:使用该属性可以使得变量或者结构体成员使用最小的对齐方式,即对变量是一字节对齐,对域(field)是位对齐。

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