关于linux内核中的__attribute__ ((packed))

news/2024/6/3 6:20:18 标签: linux内核, struct, 编译器, function, 优化, c
cle class="baidu_pl">
cle_content" class="article_content clearfix">
content_views" class="htmledit_views">

来源:
http://jimmy-lee.blog.hexun.com/8001013_d.html

__attrubte__ ((packed)) 的作用就是告诉class="tags" href="/tags/BianYiQi.html" title=编译器>编译器取消结构在编译过程中的class="tags" href="/tags/YouHua.html" title=优化>优化对齐,按照实际占用字节数进行对齐。
#define __u8 unsigned char
#define __u16 unsigned short
/* __attribute__ ((packed)) 的位置约束是放于声明的尾部“;”之前 */
class="tags" href="/tags/STRUCT.html" title=struct>struct str_class="tags" href="/tags/STRUCT.html" title=struct>struct{
__u8 a;
__u8 b;
__u8 c;
__u16 d;
} __attribute__ ((packed));
/* 当用到typedef时࿰c;要特别注意__attribute__ ((packed))放置的位置࿰c;相当于:
* typedef class="tags" href="/tags/STRUCT.html" title=struct>struct str_stuct str;
* 而class="tags" href="/tags/STRUCT.html" title=struct>struct str_class="tags" href="/tags/STRUCT.html" title=struct>struct 就是上面的那个结构。
*/
typedef class="tags" href="/tags/STRUCT.html" title=struct>struct {
__u8 a;
__u8 b;
__u8 c;
__u16 d;
} __attribute__ ((packed)) str;
/* 在下面这个typedef结构中࿰c;__attribute__ ((packed))放在结构名str_temp之后࿰c;其作用是被忽略的࿰c;注意与结构str的区别。*/
typedef class="tags" href="/tags/STRUCT.html" title=struct>struct {
__u8 a;
__u8 b;
__u8 c;
__u16 d;
}str_temp __attribute__ ((packed));
typedef class="tags" href="/tags/STRUCT.html" title=struct>struct {
__u8 a;
__u8 b;
__u8 c;
__u16 d;
}str_nopacked;
int main(void)
{
printf("sizeof str = %d/n", sizeof(str));
printf("sizeof str_class="tags" href="/tags/STRUCT.html" title=struct>struct = %d/n", sizeof(class="tags" href="/tags/STRUCT.html" title=struct>struct str_class="tags" href="/tags/STRUCT.html" title=struct>struct));
printf("sizeof str_temp = %d/n", sizeof(str_temp));
printf("sizeof str_nopacked = %d/n", sizeof(str_nopacked));
return 0;
}
编译运行:
[root@localhost root]# ./packedtest
sizeof str = 5
sizeof str_class="tags" href="/tags/STRUCT.html" title=struct>struct = 5
sizeof str_temp = 6
sizeof str_nopacked = 6
--------------------------------------------------------------------
GNU C的一大特色就是__attribute__机制。__attribute__可以设置函数属性(Function Attribute)、变量属性(Variable Attribute)和类型属性(Type Attribute)。
__attribute__书写特征是:__attribute__前后都有两个下划线࿰c;并且后面会紧跟一对括弧࿰c;括弧里面是相应的__attribute__参数。
__attribute__语法格式为:
__attribute__ ((attribute-list))
其位置约束:放于声明的尾部“;”之前。
函数属性(Function Attribute):函数属性可以帮助开发者把一些特性添加到函数声明中࿰c;从而可以使class="tags" href="/tags/BianYiQi.html" title=编译器>编译器在错误检查方面的功能更强大。__attribute__机制也很容易同非GNU应用程序做到兼容之功效。
GNU CC需要使用 –Wallclass="tags" href="/tags/BianYiQi.html" title=编译器>编译器来击活该功能࿰c;这是控制警告信息的一个很好的方式。
packed属性:使用该属性可以使得变量或者结构体成员使用最小的对齐方式࿰c;即对变量是一字节对齐࿰c;对域(field)是位对齐。

本文来自: (www.91linux.com) 详细出处参考:http://www.91linux.com/html/article/kernel/20090417/16510

cle>

http://www.niftyadmin.cn/n/1738008.html

相关文章

搜狗输入法输入英文时有提示

在搜狗中文输入法状态下,用ctrlshiftE

转:HTTP 301 跳转和302跳转的区别

301和302 Http状态有啥区别?301,302 都是HTTP状态的编码,都代表着某个URL发生了转移,不同之处在于: 301 redirect: 301 代表永久性转移(Permanently Moved), 302 redirect: 302 代表暂时性转移(Temporarily…

win10下Jupyter NoteBook添加Anaconda创建的虚拟环境

1.安装Anacoda: *注*:尽量选择安装3.6版本 ①.官网:https://www.anaconda.com/download/ ②.清华大学开源软件镜像站:https://mirrors.tuna.tsinghua.edu.cn/help/anaconda/ 2.Anacoda安装好之后,创建一个虚拟环境&#xff08…

shell中如何进行一段代码的注释

在shell编程中,我们常常需要把一些语句注释掉,让它不执行,对单号或者少数几行shell脚本来说,在每行前面增加#符号就可以达到目的了,代码如下: #cp ./a.txt ./b.txt#mkdir -p {1,2,4}/{2,4,6}#echo "o…

shell中字符串的比较

#!/bin/sh#测试各种字符串比较操作。#shell中对变量的值添加单引号,爽引号和不添加的区别:对类型来说是无关的,即不是添加了引号就变成了字符串类型,#单引号不对相关量进行替换,如不对$符号解释成变量引用,…

Anaconda Navigator打不开 This application failed to start because it could not find or load the QT ....

anaconda Navigator打不开,直接弹出错误窗口 在网上试了很多方法都没有成功,折腾了好久 卸载重装还是一样的错误 手动添加过如下的环境变量,网上有的人成功了但是我没有,还是报错 下面分享以下我最后解决的方法给大家参考 1.删除…

别把typedef当作宏使用

在开发过程中,我们常常需要自定义数据类型,比如自定义结构体等,为了代码的可读性,我们对自定义的类型常常会利用typedef关键字进行重新类型定义。比如: typedef struct _item_t{int iUin;char sName[50];...} item_t;…