C语言__attribute__的使用

2023-05-16

一、介绍

GNU C 的一大特色就是__attribute__ 机制。__attribute__ 可以设置函数属性(Function Attribute )、变量属性(Variable Attribute )和类型属性(Type Attribute )。

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

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

关键字__attribute__ 也可以对结构体(struct )或共用体(union )进行属性设置。大致有六个参数值可以被设定,即:aligned, packed, transparent_union, unused, deprecated 和 may_alias 。

在使用__attribute__ 参数时,你也可以在参数的前后都加上“__” (两个下划线),例如,使用__aligned__而不是aligned ,这样,你就可以在相应的头文件里使用它而不用关心头文件里是否有重名的宏定义。

二、__attribute__ 的参数介绍

1、aligned 

指定对象的对齐格式(以字节为单位),如:

struct S {

short b[3];

} __attribute__ ((aligned (8)));


typedef int int32_t __attribute__ ((aligned (8)));

该声明将强制编译器确保(尽它所能)变量类 型为struct S 或者int32_t 的变量在分配空间时采用8字节对齐方式。

如上所述,你可以手动指定对齐的格式,同样,你也可以使用默认的对齐方式。如果aligned 后面不紧跟一个指定的数字值,那么编译器将依据你的目标机器情况使用最大最有益的对齐方式。例如:

struct S {

short b[3];

} __attribute__ ((aligned));

这里,如果sizeof (short )的大小为2byte,那么,S 的大小就为6 。取一个2 的次方值,使得该值大于等于6 ,则该值为8 ,所以编译器将设置S 类型的对齐方式为8 字节。

aligned 属性使被设置的对象占用更多的空间,相反的,使用packed 可以减小对象占用的空间。

需要注意的是,attribute 属性的效力与你的连接器也有关,如果你的连接器最大只支持16 字节对齐,那么你此时定义32 字节对齐也是无济于事的。

2、packed

        使用该属性对struct 或者union 类型进行定义,设定其类型的每一个变量的内存约束。就是告诉编译器取消结构在编译过程中的优化对齐(使用1字节对齐),按照实际占用字节数进行对齐,是GCC特有的语法。这个功能是跟操作系统没关系,跟编译器有关,gcc编译器不是紧凑模式的,我在windows下,用vc的编译器也不是紧凑的,用tc的编译器就是紧凑的。

        下面的例子中,packed_struct 类型的变量数组中的值将会紧紧的靠在一起,但内部的成员变量s 不会被“pack” ,如果希望内部的成员变量也被packed 的话,unpacked-struct 也需要使用packed 进行相应的约束。

struct unpacked_struct
{
      char c;
      int i;
};
         
struct packed_struct
{
     char c;
     int  i;
     struct unpacked_struct s;
}__attribute__ ((__packed__));

如:

在TC下:struct my{ char ch; int a;}      sizeof(int)=2;sizeof(my)=3;(紧凑模式)

在GCC下:struct my{ char ch; int a;}     sizeof(int)=4;sizeof(my)=8;(非紧凑模式)

在GCC下:struct my{ char ch; int a;}__attrubte__ ((packed))        sizeof(int)=4;sizeof(my)=5

下面的例子中使用__attribute__ 属性定义了一些结构体及其变量,并给出了输出结果和对结果的分析。代码为:

struct p
{
    int a;
    char b;
    short c;

}__attribute__((aligned(4))) pp;

struct m
{
    char a;
    int b;
    short c;
}__attribute__((aligned(4))) mm;

struct o
{
    int a;
    char b;
    short c;
}oo;

struct x
{
    int a;
    char b;
    struct p px;
    short c;
}__attribute__((aligned(8))) xx;

int main()
{           
    printf("sizeof(int)=%d,sizeof(short)=%d.sizeof(char)=%d\n",sizeof(int)
                                                ,sizeof(short),sizeof(char));
    printf("pp=%d,mm=%d \n", sizeof(pp),sizeof(mm));
    printf("oo=%d,xx=%d \n", sizeof(oo),sizeof(xx));
    return 0;
}

输出结果:

sizeof(int)=4,sizeof(short)=2.sizeof(char)=1

pp=8,mm=12

oo=8,xx=24

分析:都是字节对齐的原理,可以去看这儿:字节对齐

3、at

绝对定位,可以把变量或函数绝对定位到Flash中,或者定位到RAM。

1)、定位到flash中,一般用于固化的信息,如出厂设置的参数,上位机配置的参数,ID卡的ID号,flash标记等等

const u16 gFlashDefValue[512] __attribute__((at(0x0800F000))) = {0x1111,0x1111,0x1111,0x0111,0x0111,0x0111};//定位在flash中,其他flash补充为00
const u16 gflashdata__attribute__((at(0x0800F000))) = 0xFFFF;

 

2)、定位到RAM中,一般用于数据量比较大的缓存,如串口的接收缓存,再就是某个位置的特定变量

u8 USART2_RX_BUF[USART2_REC_LEN] __attribute__ ((at(0X20001000)));//接收缓冲,最大USART_REC_LEN个字节,起始地址为0X20001000.


注意:

1)、绝对定位不能在函数中定义,局部变量是定义在栈区的,栈区由MDK自动分配、释放,不能定义为绝对地址,只能放在函数外定义。

2)、定义的长度不能超过栈或Flash的大小,否则,造成栈、Flash溢出。

4、section

        提到section,就得说RO RI ZI了,在ARM编译器编译之后,代码被划分为不同的段,RO Section(ReadOnly)中存放代码段和常量,RW Section(ReadWrite)中存放可读写静态变量和全局变量,ZI Section(ZeroInit)是存放在RW段中初始化为0的变量。
        于是本文的大体意思就清晰了,__attribute__((section("section_name"))),其作用是将作用的函数或数据放入指定名为"section_name"对应的段中。

1)、编译时为变量指定段:

__attribute__((section("name")))
RealView Compilation Tools for µVision Compiler Reference Guide Version 4.0 
 
Home > Compiler-specific Features > Variable attributes > __attribute__((section("name"))) 
 
4.5.6. __attribute__((section("name")))
Normally, the ARM compiler places the objects it generates in sections like data and bss. However, you might require additional data sections or you might want a variable to appear in a special section, for example, to map to special hardware. The section attribute specifies that a variable must be placed in a particular data section. If you use the section attribute, read-only variables are placed in RO data sections, read-write variables are placed in RW data sections unless you use the zero_init attribute. In this case, the variable is placed in a ZI section.
 
Note
This variable attribute is a GNU compiler extension supported by the ARM compiler.
 
Example
/* in RO section */
const int descriptor[3] __attribute__ ((section ("descr"))) = { 1,2,3 };
/* in RW section */
long long rw[10] __attribute__ ((section ("RW")));
/* in ZI section *
long long altstack[10] __attribute__ ((section ("STACK"), zero_init));/

2)、编译时为函数指定段

__attribute__((section("name")))
RealView Compilation Tools for µVision Compiler Reference Guide Version 4.0 
 
Home > Compiler-specific Features > Function attributes > __attribute__((section("name"))) 
 
4.3.13. __attribute__((section("name")))
The section function attribute enables you to place code in different sections of the image.
 
Note
This function attribute is a GNU compiler extension that is supported by the ARM compiler.
 
Example
In the following example, Function_Attributes_section_0 is placed into the RO section new_section rather than .text.
 
void Function_Attributes_section_0 (void) __attribute__ ((section ("new_section")));
void Function_Attributes_section_0 (void)
{
    static int aStatic =0;
    aStatic++;
}
In the following example, section function attribute overrides the #pragma arm section setting.
 
#pragma arm section code="foo"
  int f2()
  {
      return 1;
  }                                  // into the 'foo' area

  __attribute__ ((section ("bar"))) int f3()
  {
      return 1;
  }                                  // into the 'bar' area

  int f4()
  {
      return 1;
  }                                  // into the 'foo' area
#pragma arm section

5、多个属性,组合使用

u8 FileAddr[100] __attribute__ ((section ("FILE_RAM"), zero_init,aligned(4)));    

 

转载自:

https://www.cnblogs.com/embedded-linux/p/5801999.html

https://blog.csdn.net/weixin_38233274/article/details/82188997

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C语言__attribute__的使用 的相关文章

随机推荐

  • 算法的分类

    算法有多种分类方式 xff0c 可以根据实现方式分类 xff0c 也可以根据设计方法分类 xff0c 还可以根据应用领域进行分类 不同的分类方式有不同的特点 按照实现方式分类 xff0c 可以将算法分为递归算法 迭代算法 逻辑算法 串行算法
  • Eclipse搭建stm32+jlink开发环境全攻略(进阶篇二)

    Eclipse搭建stm32 43 jlink开发环境全攻略 进阶篇 二 我们设计程序往往会遇到这样的一个需求 xff0c 那就是我们的程序起始位置需要重新定位 xff0c 并不是默认的0x08000000 xff0c 这种情况往往出现在有
  • 不要在小公司做底层软件开发

    在这里makekam对底层软件的理解就是指驱动开发 xff0c 代码移植等工作 其中也包括底层的算法 在小公司做软件不要做底层软件开发 xff0c 犹如在公司做硬件开发不要只是焊接电路板 小公司处于产业链的最末端 xff0c 没有自己的核心
  • 多旋翼飞控篇新手课堂教程(共九集)

    多旋翼飞控篇新手课堂第一课 xff0c 将你的NAZA M真正升级成NAZA V2 http www mxkong com thread 134 1 1 html 出处 模型控MxKong 多旋翼飞控篇新手课堂第二课 xff1a NAZA远
  • Java基础final详解

    final中文意思 最后的 最终的 final 可以修饰类 属性 方法和局部变量 1 当不希望类被继承时 可以用final修饰 final class A 不可被继承 2 当不希望父类的某个方法被子类覆盖 重写 override 时 可以用
  • socket编程总结

    socket编程总结 主机字节序和网络字节序 字节序分为大端字节序 xff08 big endian xff09 和小端字节序 xff08 little endian xff09 大端字节序 xff1a 一个整数的高位存在内存的底地址 xf
  • c++中的常见问题

    CSP J终于考完了啊 xff01 坐在考场 xff0c 是一种煎熬 xff1a 为什么那么多不会啊 xff01 xff01 xff01 这里 xff0c 总结一下在c 43 43 中的那些常见问题 xff08 作者亲身经历 xff09 x
  • 大陆毫米波雷达ARS408-21xx(内附毫米波雷达使用说明书)使用记录:第一期

    文章目录 前言一 硬件链接二 代码如何使用三 大陆毫米波雷达ARS408 21XX解析代码说明总结 前言 从我个人的学习成长历程来看 xff0c 从0到1确实很难 我个人在对这款毫米波雷达的学习的过程中也比较痛苦 xff0c 资料缺乏 xf
  • 2022年度GitHub最火的力扣算法刷题宝典,手把手教你如何刷力扣~

    前言 昨晚逛了逛GitHub xff0c 无意中看到一位P8大佬的算法刷题笔记 xff0c 感觉发现了宝藏 xff01 有些小伙伴可能已经发现了 xff0c 但咱这里还是忍不住安利一波 xff0c 怕有些小伙伴没有看到 关于算法刷题的困惑和
  • LIO-SAM学习与运行测试数据集

    文章目录 0 说明0 1 环境配置说明0 2 LIO SAM0 3 系统架构0 4 LIO SAM youtube视频演示 xff1a Rotation Dataset Walking Dataset Park Dataset Campus
  • 理解ROS:参数服务器和动态参数

    文章目录 1 程序中的getparam与param1 1 getparam xff08 无默认值 xff09 1 2 param xff08 有默认值 xff09 1 3 删除参数1 4 程序中设置参数1 5 检查参数1 6 搜索参数 2
  • ROS理解:ros中的坐标以及对tf2进行解读

    文章目录 1 ROS中的坐标2 tf到tf2的变化3 发布静态tf24 发布动态tf25 监听tf26 增加自己的frame 官网就是最好的教程 xff0c 如果阅读英文没什么压力 xff0c 强烈推荐以下链接进行全面了解 xff1a 官网
  • gtsam:从入门到使用

    文章目录 一 总览二 贝叶斯网络和因子图三 机器人运动建模3 1 使用因子图建模3 2 建立因子图3 3 因子图与变量3 4 GTSAM中的非线性优化3 5 全后验推论 四 机器人定位4 1 一元测量因子4 2 自定义因子4 3 使用自定义
  • 马尔可夫链、隐马尔科夫模型、贝叶斯网络、因子图

    文章目录 一 马尔可夫链以及隐马尔可夫模型1 1 概念1 2 举例说明隐马尔可夫模型 二 贝叶斯网络三 因子图 贝叶斯网络是很多概率模型的基础 xff0c 对于slam研究也是一项必须掌握的数学理论工具 一 马尔可夫链以及隐马尔可夫模型 1
  • 3D打印机DIY之一------Prusa i3的材料清单和总体结构组装

    自己使用铝件和亚克力板组装了一台Prusa i3 3D打印机 xff0c 现在把详细的过程记录下来 总体效果图 xff1a 一 材料清单 元件数量总价2020欧式铝方管 xff1a 4根400mm 3根340mm 1根150mm 1根130
  • 位置式PID与增量式PID的介绍和代码实现

    PID分为位置式PID与增量式PID 一 位置式PID 1 表达式为 xff1a 2 缺点 xff1a 1 xff09 由于全量输出 xff0c 所以每次输出均与过去状态有关 xff0c 计算时要对ek进行累加 xff0c 工作量大 xff
  • 常见蓝牙模块介绍和AT指令

    目录 一 HC 05主从一体蓝牙模块 二 HC 06从机蓝牙模块 三 低功耗BLE蓝牙4 0模块 cc2540或cc2541 四 JDY 10 蓝牙4 0 BLE模块 五 蓝牙模块LAYOUT注意事项 xff1a 常见的蓝牙模块为 xff1
  • PID参数调节总结

    原文链接 xff1a 点击打开链接 经验 xff1a 1 采样频率低 xff08 如500ms xff09 xff0c Kp一般是0 01级别 xff1b 采样频率高 xff08 如1ms xff09 xff0c Kp一般是1级别 2 先只
  • 为Keil添加注释的快捷键

    Keil刚装上是没有注释快捷键的 xff0c 可以自己添加 xff0c Edit Configuration xff0c 然后选择 Shortcut Keys 标签页 xff0c 下拉找到 Comment Selection xff0c 然
  • C语言__attribute__的使用

    一 介绍 GNU C 的一大特色就是 attribute 机制 attribute 可以设置函数属性 xff08 Function Attribute xff09 变量属性 xff08 Variable Attribute xff09 和类